home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / applications / wp / dvi2lj.lha / dvi2xx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-14  |  147.8 KB  |  4,784 lines

  1. #define VERSION "0.51alpha11"
  2. /**********************************************************************
  3.  ****************************  Intro  *********************************
  4.  **********************************************************************
  5.  * This program translates TeX's DVI-Code into device dependent
  6.  * code of either the
  7.  *
  8.  *     -   HP-LASERJET+ and compatibles (PCL), or the
  9.  *     -   IBM 3812 pageprinter
  10.  *
  11.  * depending on the preprocessor switches specified before compilation.
  12.  * The program is written to run on a PC XT/AT/PS2 under MS-DOS. It can
  13.  * be compiled nicely with MSC Rel. 3.0-5.1 with option -AL (large memory
  14.  * model).  Take care that in the CONFIG.SYS file the FILES parameter
  15.  * is set to 20; otherwise reduce MAXOPEN.  640K are recommended.
  16.  * I use link option /stack:9000 to increase runtime stack.
  17.  * It also works without modifications under Unix System V.
  18.  **********************************************************************
  19.  *            Adapted for the PC:    Gustaf Neumann
  20.  *            +1002 stuff        University of Economics
  21.  *            +3812 support      Augasse 2-6
  22.  *            +Output buffering      A-1090 Vienna, AUSTRIA
  23.  *            +lpt binary support    Tel. *43-222-340525/533
  24.  *            +pk-89 stuff                   773
  25.  *            +pixelpaths
  26.  *            +alternative directory structure
  27.  *            +code compiles also under Unix V (HP/UX)
  28.  *                      (thx Michael Haberler)
  29.  *            +huge characters (a character bigger than 32K)
  30.  *                      formats PXL1001 and PXL 1002
  31.  *                     (use: raster graphics)
  32.  *            +reduction of the produced code
  33.  *            +new options -X -Y -c -g
  34.  *            +changed options -r (LJ now default from first to last)
  35.  *                     -x,-y  (accept floats)
  36.  *            +new option -z for LJ: print testpage containing
  37.  *                     pagecounter after printjob
  38.  *            +try to overcome font limit on LJ (max 16 fonts/page) and
  39.  *             (max 32 fonts/document):
  40.  *                     additional fonts are drawn as bitmap-
  41.  *                     graphics.
  42.  *            +allows to set character close to the paperedge on LJ
  43.  *            +gf-supprt (by Joe Kelsey joe@Pacer.com)
  44.  *                gf.c and gf.h from mitdevices/dvi2ps
  45.  *            +clipping of rules
  46.  *            +OS/2 defines from Rutger Berns, apprmb@hheouh50.bitnet
  47.  *
  48.  *            BITNET/EARN:       NEUMANN at AWIWUW11
  49.  **********************************************************************
  50.  * fixes in LJ-mode:  rule-drawing,
  51.  *            characters with 127<=height<=200
  52.  *            reset printer at beginning and end of each job
  53.  *            better positioning of rules
  54.  * 30.1.89 (0.48) bug fixed for files containing >32 fonts (thanks A. Brosig),
  55.  *                different font assignment heuristic
  56.  * fixes in 3812-mode:  14.juli 87  positioning of rastered characters
  57.  *            better positioning of rules
  58.  * general fixes
  59.  * 1.1.88         page origin set to 1in/1in (hopefully everywhere)
  60.  * 22.7.88        reset y-position for each page-eject
  61.  * 15.1.89        fixing bug is space allocation for EmitFileName
  62.  *                (thanks to Bernhard Simon)
  63.  * 15.3.91 (0.49) landscape support for lj ii p, lj iii and lj 2000 
  64.  *                fixing rule drawing problems for IBM3812 in landcape mode,
  65.  *                256 character clean (lj family and IBM3812) 
  66.  * 5.5.91 (0.50)  -DSEVENBIT added for older LJ-emulations
  67.  *                -D1, -D2, -D-, -D1-, -D2- options added due to suggestions 
  68.  *                from Tomasz Wolniewicz
  69.  * February 1995  added postscript support (AMIGA only) 
  70.  *                Peter A. Henning (P.Henning@gsi.de)
  71.  *              
  72.  **********************************************************************
  73.  * Preprocessor switches:
  74.  *      #define DEBUG    for massive printing of trace information
  75.  *               when -d cmdline option specified
  76.  *      #define IBM3812  produce output for the IBM3812 pageprinter
  77.  *      #define LJ       produce output for the HP Laserjet+ or LJ II
  78.  *      #define LJ2P     produce output for the HP Laserjet LJ IIP, LJ III
  79.  *                       or LaserJet 2000
  80.  *      #define LJ_LARGE_FONT_MEMORY  large FONT Memory for LJ printer family
  81.  *      #define DRAWGLYPH draws PK-Glyphs on stderr
  82.  *      #define USEPXL   use PXL and PK fonts rather than gf fonts
  83.  */
  84. /**********************************************************************
  85.  ************************  Global Definitions  ************************
  86.  **********************************************************************/
  87. /* #define IBM3812 */
  88. /* #define LJ */
  89. /* #define DRAWGLYPH */
  90.  
  91. #ifdef KPATHSEA
  92. #include <kpathsea/config.h>
  93. #include <kpathsea/c-limits.h>
  94. #include <kpathsea/c-memstr.h>
  95. #include <kpathsea/tex-glyph.h>
  96. #include <kpathsea/tex-make.h>
  97. #endif
  98.  
  99. #include "config.h"
  100.  
  101. #include "commands.h"
  102. #include <string.h>
  103. #include <signal.h>
  104. #include <stdio.h>
  105. #include <ctype.h>
  106. #ifdef vms
  107. #include <file.h>
  108. #else
  109. #include <fcntl.h>
  110. #endif
  111. #ifdef MSDOS
  112. #include <dos.h>     /* only for binaryopen on device  */
  113. #endif
  114. #ifdef  unix
  115. #include <limits.h>
  116. #endif
  117.  
  118. #define  DVIFORMAT     2
  119. #define  UNKNOWN      -1
  120. #define  FIRSTFNTCHAR  0
  121.  
  122. #ifdef   OPEN_MAX                    /* ... in a friendly unix system  */
  123. #ifndef vms
  124. #define  MAXOPEN_OS    (OPEN_MAX - 8)
  125. #else
  126. #define  MAXOPEN_OS    12     /* OPEN_MAX seems to be 8 on VMS systems */
  127. #endif
  128. #else
  129. #define  MAXOPEN_OS    12     /* limit on number of open font files */
  130. #endif
  131.  
  132. #ifdef LJ_RESIDENT_FONTS
  133. /* we have to read tfm files as well */
  134. #define  MAXOPEN       (MAXOPEN_OS - 1)
  135. #else
  136. #define  MAXOPEN       MAXOPEN_OS
  137. #endif
  138.  
  139. #define  NFNTCHARS       LASTFNTCHAR+1
  140. #define  STACK_SIZE      100     /* DVI-stack size                     */
  141. #define  NONEXISTANT     -1      /* offset for PXL files not found     */
  142. #define  NO_FILE        ((FILE *)-1)
  143. #define  NEW(A) ((A *)  malloc(sizeof(A)))
  144. #define  EQ(a,b)        (strcmp(a,b)==0)
  145. #define  MM_TO_PXL(x)   (int)(((x)*RESOLUTION*10)/254)
  146. #define  PT_TO_PXL(x)   (int)((long)((x)*RESOLUTION*100l)/7224)
  147. #define  PT_TO_DVI(x)   (long)((x)*65536l)
  148. #define  BOPENCMD fopen
  149. #define  BINOPEN(f) BOPENCMD(f,READ_BINARY)
  150. /* SMALL_SIZE characters are loaded into font storage of the printer   */
  151. /* LARGE_SIZE characters are rastered                                  */
  152. /* HUGE_SIZE characters are not loaded into the memory of the host     */
  153. #define  SMALL_SIZE (unsigned char) 0
  154. #define  LARGE_SIZE (unsigned char) 1
  155. #define  HUGE_SIZE  (unsigned char) 2
  156. #define  HUGE_CHAR_PATTERN 32767l
  157. #define  BYTES_PER_PIXEL_LINE 500    /* max number of bytes per pixel line */
  158.  
  159.  
  160. #define PK_POST 245
  161. #define PK_PRE 247
  162. #define PK_ID 89
  163.  
  164. /* to speedup the program a little: redefinition of PixRound and PutWord */
  165. /*#define PIXROUND(x,c) ((((double)x+(double)(c>>1))/(double)c)+0.5)*/
  166. #define PIXROUND(x,c) (((x)+c)/c)
  167. #define PUTWORD(w)  EMITC((char)(w>>8)&0xff); EMITC((char)w&0xff)
  168. /*************************************************************************/
  169. #define  EMIT            fprintf              /* output a formatted string   */
  170. #ifndef vms
  171. # define  EMITB(len,b)   fwrite(b,1,len,outfp)  /* output binary data of len */
  172. #else
  173.     /* VMS doesn't like to use fwrite on a file with fixed record sizes,
  174.        so use number of putc calls */
  175. # define  EMITB(len,b)   for (kk = 0;kk < len; kk++) putc(*(b+kk),outfp);
  176. #endif
  177. #define  EMITWORD(w)     PUTWORD((w))        /* output a 2 byte word of data */
  178.  
  179. #define  MoveOver(b)  h += (long) b
  180. #define  MoveDown(a)  v += (long) a
  181. #define  qfprintf if (!G_quiet) fprintf
  182. #define  qprintf  if (!G_quiet) printf
  183. #define  LARGER(a,b) (((a)>(b)) ? (a) : (b))
  184.  
  185. #ifdef IBM3812
  186. #define  PRINTER      "IBM 3812 pageprinter"
  187. #define  EMITC(c)      PMPoutC(c)               /* output a single character */
  188. #define  PMPcont(l)    PMPout(-1,(long)l)         /* next l bytes continuous */
  189. #define  PMPflush      PMPout(0l,"")                     /* flush PMP-buffer */
  190. #define  EMITL(l,d)    PMPout((int)l,d)      /* EMIT-logical: via PMP-buffer */
  191. #define  hconvRESOLUTION   240
  192. #define  vconvRESOLUTION   240
  193. #define  CHAR_WIDTH_LARGE  100       /*  limit for loading into printer font */
  194. #define  CHAR_HEIGTH_LARGE 127       /*  limit for loading into printer font */
  195. #define  OUTBUFSIZE     20000        /*   size of output buffer for PMP cmds */
  196.                       /*   has to be less max(signed int)     */
  197. #define  MAXFONTSTORAGE      130000l /* font storage in the 3812 pageprinter */
  198. #define  EMITFILE_EXTENSION    ".pmp"      /* default extension of emit file */
  199. #define  XDEFAULTOFF    RESOLUTION        /* y default offset on page 1 inch */
  200. #define  YDEFAULTOFF    RESOLUTION        /* y default offset on page 1 inch */
  201. #define  CHARSTRINGMAX  80                /* bufferlength for SetString      */
  202. #define  MAX_PAGE_WIDTH  2040
  203. #define  MAX_PAGE_HEIGHT 3360
  204. /**********************************************************************/
  205. /**************  Positioning for the 3812  ****************************/
  206. /**********************************************************************/
  207. #define VERT_HALF(n) ((short)((n+1)>>1)-1)
  208. #define HOR_HALF(n)  ((short)(n>>1))
  209. #define MoveHor(n)  if ((n)!=0) { PMPcont(3); PMPout(1,"\342"); EMITWORD((n)); }
  210. #define MoveVert(n) if ((n)!=0) { PMPcont(3); PMPout(1,"\343"); EMITWORD((n)); }
  211. #endif
  212.  
  213. #ifdef LJ
  214. # ifdef LJ4
  215. #  define  PRINTER       "HP Laserjet IV"
  216. # else
  217. #  ifdef LJ2P
  218. #   define  PRINTER       "HP LaserJet IIP"
  219. #  else
  220. #   ifdef LJ2 
  221. #    define  PRINTER       "HP LaserJet II"
  222. #   else
  223. #    define  PRINTER       "HP LaserJet"
  224. #   endif
  225. #  endif
  226. # endif
  227.  
  228. # ifdef LJ4
  229. int   RESOLUTION = 300;
  230. char *MFMODE     = MFMODE300;
  231. # else
  232. #  define RESOLUTION 300
  233. # endif
  234. # define  hconvRESOLUTION   RESOLUTION
  235. # define  vconvRESOLUTION   RESOLUTION
  236. # ifdef LJ2
  237. /* the printer limit of the LJ2P is actually 16384x16384, 
  238.   * but to exploit it, one would need lots of memory in the printer
  239.  */
  240. #  define  CHAR_WIDTH_LARGE  200     /* limit for loading into printer font */
  241. #  define  CHAR_HEIGTH_LARGE 255         /* y_offset reaches the same size! */
  242. # else   /* such as LaserJet+, Laserjet II */
  243. #  define  CHAR_WIDTH_LARGE  100     /* limit for loading into printer font */
  244. #  define  CHAR_HEIGTH_LARGE 127         /* y_offset reaches the same size! */
  245. # endif
  246. # define  EMITFILE_EXTENSION  ".lj"       /* default extension of emit file */
  247. # ifndef MAX_FONTS_PER_PAGE
  248. #  define  MAX_FONTS_PER_PAGE 16        /* maximum number of fonts per page */
  249. # endif
  250. # define  HANDLE_MAX_FONTS  255      /* max nr of fonts handled (rasterfont) */
  251. # define  FONTS_DOWNLOADABLE 32    /* max nr of fonts that can be downloaded */
  252. # define  MAXFONTSTORAGE (395l*1024l)                /* standard user memory */
  253. # define  EMITC(c)       putc(c,outfp)          /* output a single character */
  254. # define  EMITL(l,d)     EMITB(l,d)                  /* EMIT-logical = EMITB */
  255. # ifdef LJ4
  256. #  define  XDEFAULTOFF   RESOLUTION 
  257. #  define  YDEFAULTOFF   RESOLUTION
  258. # else
  259. #  define  XDEFAULTOFF   RESOLUTION-54   /*x default offset on page 1in (LJ2)*/
  260. #  define  YDEFAULTOFF   RESOLUTION+9      /* y default offset on page 1inch */
  261. # endif
  262. # define  max(x,y)       if ((y)>(x)) x = y
  263. # ifndef vms
  264. #  define  INT_ASCII(b,i) \
  265.    if (i == 0) b[0] = '\0'; else sprintf((char *)b,"%hd",i)
  266. # else
  267. #  define  INT_ASCII(b,i) \
  268.    if (i == 0) b[0] = '\0'; else sprintf((char *)b,"%d",i)
  269. # endif
  270. #endif
  271.  
  272. #ifdef SEVENBIT
  273. #define VIS   33
  274. #define VIS2  (VIS+32)
  275. unsigned char
  276. VisChar(c)
  277. unsigned char   c;
  278. {
  279.     c &= 0xff;
  280.     if (c < VIS)
  281.         return ((unsigned char)(160 + c));
  282.     if (c < 128)
  283.         return (c);
  284.     if (c < (255 - VIS2))
  285.         return ((unsigned char)(VIS2 + c));
  286.     return (255);
  287. }
  288. #else
  289. #define VisChar(c) (unsigned char)(c)
  290. #endif
  291.  
  292.  
  293. /**********************************************************************/
  294. /***********************  external definitions  ***********************/
  295. /**********************************************************************/
  296.  
  297. #ifdef _AMIGA
  298. void    CopyPSFile();
  299. void    CopyIFFFile();
  300. #endif
  301.  
  302. #ifndef _AMIGA
  303. #ifndef unix
  304. long    access();
  305. FILE   *BOPENCMD();
  306. void    exit();
  307. int     fclose();
  308. int     fprintf();
  309. int     fseek();
  310. char   *index();
  311. int     printf();
  312. int     sscanf();
  313. int     strcmp();
  314. char   *strcpy();
  315. #ifdef MSC5
  316. unsigned int strlen();
  317. #endif
  318. void    free();
  319. void    setbuf();
  320. #endif
  321.  
  322. #ifdef MSDOS
  323. int     intdos();
  324. #else
  325. char   *getenv();
  326. #endif
  327. #endif
  328.  
  329.  
  330. #ifndef USEPXL
  331. /* interface to gf.c */
  332. extern FILE *gfin;
  333. extern int checksum;
  334. extern long tfm_wd[], char_pointer[];
  335. extern char char_exists[];
  336. extern int num_cols, num_rows, num_bytes, x_offset, y_offset;
  337. extern unsigned char bits[];
  338. extern int gf_font_max_m, gf_font_max_n, gf_font_min_n;
  339. extern int gettochar();
  340. extern void readbits();
  341. extern void readpost();
  342. extern void seekpost();
  343. extern int seekchar();
  344. #endif
  345.  
  346.  
  347. /**********************************************************************/
  348. /*************************  Global Procedures  ************************/
  349. /**********************************************************************/
  350. /* Note: Global procedures are declared here in alphabetical order, with
  351.    those which do not return values typed "void".  Their bodies occur in
  352.    alphabetical order following the main() procedure.  The names are
  353.    kept unique in the first 6 characters for portability. */
  354. double  ActualFactor();
  355. void    AllDone();
  356. #ifdef  MSDOS
  357. void    AssureBinary();  /* DOS and Microsoft C dependent !!! */
  358. #endif
  359. void    CopyFile();
  360. void    CopyHPFile();
  361. void    DecodeArgs();
  362. void    DoBop();
  363. long    DoConv();
  364. void    DoSpecial();
  365. void    EmitChar();
  366. void    Fatal();
  367. void    FindPostAmblePtr();
  368. void    FormFeed();
  369. void    GetBytes();
  370. void    GetFontDef();
  371. char    *GetKeyStr();
  372. bool    GetKeyVal();
  373. bool    IsSame();
  374. void    lcase();
  375. void    LoadAChar();
  376. long    NoSignExtend();
  377. /* see cautionary note in code, re arithmetic vs logical shifts */
  378. void    OpenFontFile();
  379. long    PixRound();
  380. void    PkRaster();
  381. void    PutWord();
  382. void    RasterLine();
  383. void    RasterChar();
  384. void    ReadFontDef();
  385. void    ReadPostAmble();
  386. void    SetChar();
  387. void    SetFntNum();
  388. void    SetPosn();
  389. void    SetRule();
  390. void    SetString();
  391. long    SignExtend();
  392. /* see cautionary note in code, re arithmetic vs logical shifts */
  393. void    SkipFontDef();
  394. void    Warning();
  395. unsigned char   skip_specials() ;
  396.  
  397. #ifdef IBM3812
  398. void    PMPout();
  399. void    PMPoutC();
  400. #endif
  401.  
  402. /**********************************************************************/
  403. /***********************  Font Data Structures  ***********************/
  404. /**********************************************************************/
  405.  
  406. struct char_entry {             /* character entry */
  407. #ifdef USEPXL
  408.     unsigned short  width, height;      /* width and height in pixels */
  409.     short   xOffset, yOffset, yyOffset; /* x offset and y offset in pixels*/
  410. #endif
  411.     struct {
  412.         bool isloaded;
  413.         union {
  414.             long    fileOffset;
  415.             long    *pixptr;
  416.         } address;
  417.     } where;
  418.     long    tfmw;             /* TFM width                 */
  419.     long    cw;               /* character width in pixels */
  420.     unsigned char   flag_byte;          /* for PK-files    */
  421.     unsigned char   charsize;
  422. };
  423. struct font_entry {    /* font entry */
  424.     long    k, c, s, d;
  425.     int     a, l;
  426.     char n[STRSIZE];          /* FNT_DEF command parameters                */
  427.     long    font_mag;         /* computed from FNT_DEF s and d parameters  */
  428.     /*char psname[STRSIZE];*/ /* PostScript name of the font               */
  429.     char    name[STRSIZE];    /* full name of PXL file                     */
  430.     FILE * font_file_id;      /* file identifier (NO_FILE if none)         */
  431. #ifdef USEPXL
  432.     long    magnification;    /* magnification read from PXL file          */
  433.     long    designsize;       /* design size read from PXL file            */
  434. #endif
  435.     struct char_entry ch[NFNTCHARS];   /* character information            */
  436.     struct font_entry *next;
  437.     unsigned short ncdl;      /* #of different chars actually downloaded   */
  438.     unsigned short plusid;    /* Font id in Printer                        */
  439.     bool used_on_this_page;
  440. #ifdef LJ_RESIDENT_FONTS
  441.     bool resident_p;          /* is font resident in printer?              */
  442.     char symbol_set[40];      /* symbol set value (resident fonts)         */
  443.     unsigned short resid;     /* typeface id (resident fonts)              */
  444.     unsigned spacing;         /* 0=monospace, 1=variable (resident fonts)  */
  445.     unsigned style;           /* upright/italic/... (resident fonts)       */
  446.     int weight;               /* regular/bold/... (resident fonts)         */
  447.     double pitch;             /* chars per inch (monospaced resident fonts)*/
  448. #endif
  449.     enum PxlId {
  450.         id1001, id1002, pk89    } id;
  451. #ifdef LJ
  452.     unsigned short max_width, max_height, max_yoff;
  453. #endif
  454. };
  455.  
  456.  
  457. struct pixel_list {
  458.     FILE *pixel_file_id;    /* file identifier  */
  459.     int     use_count;      /* count of "opens" */
  460. };
  461. /**********************************************************************/
  462. /*************************  Global Variables  *************************/
  463. /**********************************************************************/
  464. long    FirstPage  = -1000000;  /* first page to print (uses count0)   */
  465. long    LastPage   = 1000000;   /* last page to print                  */
  466. long    PrintPages = 1000000;   /* nr of pages to print                */
  467. bool    FirstPageSpecified = _FALSE;
  468. bool    LastPageSpecified = _FALSE;
  469. char    G_progname[STRSIZE];    /* program name                        */
  470. char    filename[STRSIZE];      /* DVI file name                       */
  471. char    rootname[STRSIZE];      /* DVI filename without extension      */
  472. #ifndef KPATHSEA
  473. char    *PXLpath = FONTAREA;    /* PXL path name for search            */
  474. #endif
  475. char    *HeaderFileName = "";   /* file name & path of Headerfile      */
  476. char    *EmitFileName = "";     /* file name & path for output         */
  477. #ifdef IBM3812
  478. bool    FirstAlternate = _FALSE; /* first page from alternate casette ?   */
  479. #endif
  480. bool    Reverse = _FALSE;        /* process DVI pages in reverse order?   */
  481. bool    Landscape = _FALSE;      /* print document in ladscape mode       */
  482. bool    ResetPrinter = _TRUE;    /* reset printer at the begin of the job */
  483. bool    DoublePage = _FALSE;     /* print on both sides of a paper        */
  484. bool    PrintSecondPart = _TRUE; /* print First Part when DoublePage      */
  485. bool    PrintFirstPart  = _TRUE; /* print Second Part when DoublePage     */
  486. bool    PrintEmptyPages = _TRUE; /* print Empty pages in DoublePage mode  */
  487. short   PageParity = 1;
  488. #ifdef MAKETEXPK
  489. bool    makeTexPK = _TRUE;
  490. #endif
  491.  
  492. #ifdef LJ
  493. #ifdef LJ2P
  494. int     DuplexMode = 0;
  495. #endif
  496. #ifdef LJ4
  497. bool    econoMode = _FALSE;
  498. #endif
  499. bool    PrintTestPage = _FALSE; /* print testpage with pagecounter after job */
  500. unsigned short pagesize = 0;    /* page size value                      */
  501. unsigned short pgsiz_dots = 0;  /* page size in dots (for rule-clipping)*/
  502. #endif
  503.  
  504.  
  505. #ifndef vms
  506. short   G_errenc = 0;           /* has an error been encountered?      */
  507. #else
  508. long    G_errenc = SS$_NORMAL;  /* has an error been encountered?      */
  509. #endif
  510. bool    G_header = _FALSE;      /* copy header file to output?         */
  511. bool    G_quiet = _FALSE;       /* for quiet operation                 */
  512. bool    G_noverbatim = _TRUE;   /* inform user about pxl-files used    */
  513. bool    G_nowarn = _FALSE;      /* don't print out warnings            */
  514. short   x_origin;               /* x-origin in dots                    */
  515. short   y_origin;               /* y-origin in dots                    */
  516. short   x_goffset;              /* global x-offset in dots             */
  517. short   y_goffset;              /* global y-offset in dots             */
  518. unsigned short ncopies = 1;     /* number of copies to print           */
  519. long    hconv, vconv;           /* converts DVI units to pixels        */
  520. long    den;                    /* denominator specified in preamble   */
  521. long    num;                    /* numerator specified in preamble     */
  522. long    h;                      /* current horizontal position         */
  523. long    hh = 0;                 /* current h on device                 */
  524. long    v;                      /* current vertical position           */
  525. long    vv = 0;                 /* current v on device                 */
  526. long    mag;                    /* magnification specified in preamble */
  527. long    usermag = 0;            /* user specified magnification        */
  528. int     ndone = 0;              /* number of pages converted           */
  529. int     nopen = 0;              /* number of open PXL files            */
  530. #ifdef vms
  531. int    kk;            /* loop variable for EMITB           */
  532. #endif
  533. FILE  *outfp = NULL;            /* output file                         */
  534. FILE  *pxlfp;                   /* PXL file pointer                    */
  535. FILE  *dvifp  = NULL;           /* DVI file pointer                    */
  536. struct font_entry *prevfont = NULL; /* font_entry pointer previous font*/
  537. struct font_entry *fontptr;     /* font_entry pointer                  */
  538. struct font_entry *hfontptr = NULL; /* font_entry pointer              */
  539. struct font_entry *pfontptr = NULL; /* previous font_entry pointer     */
  540. struct pixel_list pixel_files[MAXOPEN+1]; /* list of open PXL files    */
  541. long    postambleptr;           /* Pointer to the postamble            */
  542. long    ppagep;                 /* previous page pointer               */
  543. static int      last_ry = UNKNOWN;      /* last y-position on page     */
  544. static int      last_rx = UNKNOWN;      /* last x-position on page     */
  545. long   StartPrintPages;         /* notpad for double paged output      */
  546. int    WouldPrint    = 0;
  547. bool   ZeroPage = _FALSE;       /* Document starts with a Zero Page    */
  548. bool   EvenPage = _FALSE;       /* Document starts with an even Page   */
  549. long   LastPtobePrinted = 0;
  550. int    G_ncdl = 0;
  551.  
  552. long    allocated_storage = 0; /* size of mallocated storage (statistics) */
  553. long    power[32] ;
  554. long    gpower[33] ;
  555.  
  556.  
  557. #ifdef DEBUG
  558. int Debug = 0;
  559. #endif
  560.  
  561. #ifdef LJ
  562. int   fonts_used_on_this_page = MAX_FONTS_PER_PAGE+1;
  563. char  rasterfont[HANDLE_MAX_FONTS];
  564.     /* raster if fonts/page>MAX_FONTS_PER_PAGE*/
  565. #ifdef LJ_RESIDENT_FONTS
  566. unsigned resident_count = 0;
  567. #ifndef KPATHSEA
  568. char *TFMpath = DEFAULT_TFM_PATH;
  569. #endif
  570. #endif
  571. #endif
  572.  
  573. long     used_fontstorage = 0;
  574.  
  575. #ifdef IBM3812
  576. char    PMPformat[20];
  577. char    CharString[CHARSTRINGMAX];
  578. unsigned int CharStringPos = 0;
  579. #define CharStringOut \
  580.     if (CharStringPos>0) { \
  581.         PMPcont(CharStringPos+1);\
  582.         PMPoutC((unsigned char)CharStringPos);\
  583.         PMPout(CharStringPos, CharString); \
  584.         CharStringPos=0; }
  585. #endif
  586.  
  587. #ifdef TIMING
  588. /************************timing stuff*********************/
  589. #ifdef BSD_TIME_CALLS
  590. #ifndef vms
  591. #include <sys/timeb.h>
  592. #else
  593. #include <timeb.h>
  594. #endif
  595. struct timeb timebuffer;
  596. double  start_time;
  597. #else
  598. #include <sys/time.h>
  599. struct timeval Tp;
  600. double  start_time;
  601. #endif
  602. #endif
  603.  
  604.  
  605.  
  606. /**********************************************************************/
  607. /*******************************  main  *******************************/
  608. /**********************************************************************/
  609. void
  610. main(argc, argv)
  611. int     argc;
  612. char    *argv[];
  613. {
  614.     struct stack_entry {  /* stack entry */
  615.         long    h, v, w, x, y, z;  /* what's on stack */
  616.     };
  617.     short   command;          /* current command                         */
  618.     long    count[10];        /* the 10 counters at begining of each page*/
  619.     long    cpagep = 0;       /* current page pointer                    */
  620.     bool Emitting = _FALSE;   /* outputting typsetting instructions?     */
  621.     int     i;                /* command parameter; loop index           */
  622.     int     k;                /* temporary parameter                     */
  623.     char    n[STRSIZE];       /* command parameter                       */
  624.     int     PassNo = 0;       /* which pass over the DVI page are we on? */
  625.     bool SkipMode = _FALSE;   /* in skip mode flag                       */
  626.     int     sp = 0;           /* stack pointer                           */
  627.     struct stack_entry stack[STACK_SIZE];  /* stack                      */
  628.     char    SpecialStr[STRSIZE]; /* "\special" strings                   */
  629.     long    val, val2;        /* temporarys to hold command information  */
  630.     long    w = 0;            /* current horizontal spacing              */
  631.     long    x = 0;            /* current horizontal spacing              */
  632.     long    y = 0;            /* current vertical spacing                */
  633.     long    z = 0;            /* current vertical spacing                */
  634.  
  635.  
  636. #ifdef vms
  637.     extern noshare int errno;
  638.     extern noshare char *sys_errlist[];
  639. #else
  640.     extern  char *sys_errlist[];
  641.     extern  int     errno;
  642. #endif
  643.  
  644.     x_origin = XDEFAULTOFF; /* x-origin in dots                    */
  645.     y_origin = YDEFAULTOFF; /* y-origin in dots                    */
  646.  
  647.     setbuf(stderr, NULL);
  648.     (void) strcpy(G_progname, argv[0]);
  649.     DecodeArgs( argc, argv );
  650.  
  651.  
  652. #ifdef KPATHSEA
  653.     {
  654.       /* Kpathsea initialization. */
  655.       char *makepk = getenv ("DVILJMAKEPK");
  656.       /* Might have a program-specific name for MakeTeXPK itself.  */
  657.       if (makepk)
  658.         KPSE_MAKE_SPEC_PROGRAM (kpse_make_specs[kpse_pk_format])
  659.           = makepk;
  660.       KPSE_MAKE_SPEC_ENABLED (kpse_make_specs[kpse_pk_format])
  661.         = makeTexPK;
  662.       xputenv_int ("MAKETEX_BASE_DPI", (int) (RESOLUTION + .5));
  663.       kpse_fallback_font = "cmr10";
  664.       kpse_init_fallback_resolutions ("DVILJSIZES");
  665.       kpse_font_override_path = getenv ("DVILJFONTS");
  666.       xputenv ("MAKETEX_MODE", MFMODE);
  667.     }
  668. #endif
  669.  
  670.     power [ 0 ] = 1 ;
  671.     for ( i = 1 ; i <= 31 ; i ++)
  672.         power [ i ] = power [ i - 1 ] << 1 ;
  673.     gpower[0] = 0l ;
  674.     for ( i = 1 ; i <= 32 ; i ++)
  675.         gpower[i] = gpower[i - 1] + power[i - 1] ;
  676.  
  677.     if ((i = (int) NoSignExtend(dvifp, 1)) != PRE)  {
  678.         Fatal(
  679.         "%s: PRE doesn't occur first--are you sure this is a DVI file?\n\n",
  680.                 G_progname);
  681.     }
  682.     i = (int) SignExtend(dvifp, 1);
  683.     if (i != DVIFORMAT)  {
  684.         Fatal( "%s: DVI format = %d, can only process DVI format %d files\n\n",
  685.                 G_progname, i, DVIFORMAT);
  686.     }
  687.  
  688.     if (*EmitFileName == '-')
  689.         outfp = stdout;
  690.     else
  691.         if ((outfp = fopen(EmitFileName, WRITE_BINARY)) == NULL)
  692.         Fatal("opening output file: fopen(%s) : %s",
  693.             EmitFileName,sys_errlist[errno]);
  694.  
  695. #ifdef MSDOS
  696.     AssureBinary(outfp);
  697. #endif
  698.  
  699. #ifdef TIMING
  700. #ifdef BSD_TIME_CALLS
  701.     ftime(&timebuffer);
  702.     start_time = timebuffer.time + (float)(timebuffer.millitm) / 1000.0;
  703. #else
  704.     gettimeofday(&Tp, NULL);
  705.     start_time = Tp.tv_sec + ((float)(Tp.tv_usec))/ 1000000.0;
  706. #endif
  707. #endif
  708.  
  709.     /* it is important that these be the very first things output !!! */
  710.     if ( G_header )
  711.         CopyFile( HeaderFileName );
  712.  
  713.     /*****************************/
  714.     /*for( i0=0; i0<nif; i0++ )  */    /* copy all included files */
  715.     /*    CopyFile( Ifile[i0] ); */
  716.     /*****************************/
  717.  
  718. #ifdef IBM3812
  719.     PMPout(3, "\307\310\366");          /* unload all fonts and macros */
  720.     EMITWORD(MAX_PAGE_WIDTH);
  721.     EMITWORD(MAX_PAGE_HEIGHT);
  722.     if (Landscape)
  723.         PMPout(2, "\322\1");
  724. #endif
  725. #ifdef LJ
  726.     if (ResetPrinter)
  727.       EMIT(outfp, "\033E");
  728. #ifdef LJ4
  729.     EMIT(outfp, "\033%%-12345X@PJL SET RESOLUTION=%d\012",RESOLUTION);
  730.     EMIT(outfp, "@PJL SET PAGEPROTECT=OFF\012");
  731.     EMIT(outfp, "@PJL SET ENTER LANGUAGE=PCL\012");
  732.     EMIT(outfp, "\033&u600D\033*t%dR",RESOLUTION);
  733.     if (econoMode)
  734.       EMIT(outfp, "\033*v1T");
  735. #endif
  736. #ifdef LJ2P
  737.     if (DuplexMode) 
  738.       EMIT(outfp, "\033&l%dH", DuplexMode); 
  739. #endif
  740.     if (Landscape)
  741.       EMIT(outfp, "\033&l1O\033*rF");
  742.     if (pagesize>0)
  743. #ifndef vms
  744.       EMIT(outfp, "\033&l%hdaE\033&aL", pagesize);
  745. #else
  746.       EMIT(outfp, "\033&l%daE\033&aL", pagesize);
  747. #endif
  748.     else
  749.       EMIT(outfp, "\033&lE\033&aL");
  750.  
  751.     if (ncopies>1)
  752. #ifndef vms
  753.       EMIT(outfp, "\033&l%hdX", ncopies);
  754. #else
  755.       EMIT(outfp, "\033&l%dX", ncopies);
  756. #endif
  757. #endif
  758.     if (DoublePage) {
  759.          StartPrintPages = PrintPages;
  760. #ifdef IBM3812
  761.          Reverse = (bool)!Reverse; /* perverse and strange */
  762. #endif
  763.  
  764.     }
  765.     if (Reverse) {
  766. #ifdef DEBUG
  767.         if (Debug)
  768.               fprintf(stderr, "reverse\n");
  769. #endif
  770.         ReadPostAmble(_TRUE);
  771.         fseek(dvifp, ppagep, 0);
  772.     } else {
  773.         ReadPostAmble(_TRUE);
  774.         fseek(dvifp,  14l, 0);
  775.         k = (int) NoSignExtend(dvifp, 1);
  776.         GetBytes(dvifp, n, k);
  777.     }
  778.     PassNo = 0;
  779.  
  780.     while (_TRUE)  {
  781.         command = (short) NoSignExtend(dvifp, 1)   ;
  782. #ifdef DEBUG
  783.         if (Debug)
  784.                fprintf(stderr,"CMD:\t%d\n", command);
  785. #endif
  786.         switch (command)  {
  787.         case SET1:
  788.         case SET2:
  789.         case SET3:
  790.         case SET4:
  791.             val = NoSignExtend(dvifp, (int) command - SET1 + 1);
  792.             if (!SkipMode)
  793.                 SetChar(val, command, PassNo, _TRUE,_FALSE);
  794.             break;
  795.         case SET_RULE:
  796.             val = NoSignExtend(dvifp, 4);
  797.             val2 = NoSignExtend(dvifp, 4);
  798.             if (Emitting)
  799.                 SetRule(val, val2, 1);
  800.             break;
  801.         case PUT1:
  802.         case PUT2:
  803.         case PUT3:
  804.         case PUT4:
  805.             val = NoSignExtend(dvifp, (int) command - PUT1 + 1);
  806.             if (!SkipMode)
  807.                 SetChar(val, command, PassNo, _TRUE,_FALSE);
  808.             break;
  809.         case PUT_RULE:
  810.             val = NoSignExtend(dvifp, 4);
  811.             val2 = NoSignExtend(dvifp, 4);
  812.             if (Emitting)
  813.                 SetRule(val, val2, 0);
  814.             break;
  815.         case NOP:
  816.             break;
  817.         case BOP:
  818.             cpagep = ftell(dvifp) - 1;
  819.             for (i = 0; i <= 9; i++)
  820.                 count[i] = NoSignExtend(dvifp, 4);
  821.             ppagep = NoSignExtend(dvifp, 4);
  822.             h = v = w = x = y = z = 0;
  823.             hh = vv = 0;
  824.             sp = 0;
  825.             fontptr = NULL;
  826.             prevfont = NULL;
  827.             DoBop();
  828. /*
  829. fprintf(stderr,"skimode %d, count %d, F %d, L %d\n",
  830.     (int)SkipMode,(int)count[0],(int)FirstPageSpecified,(int)LastPageSpecified);
  831. */
  832.             SkipMode = (bool) ((FirstPageSpecified && count[0] < FirstPage) || 
  833.                                               (LastPageSpecified && count[0] > LastPage ));
  834. /*
  835. fprintf(stderr,"skimode %d, count %d, F %d, L %d\n",
  836.     (int)SkipMode,(int)count[0],(int)FirstPageSpecified,(int)LastPageSpecified);
  837. */
  838.  
  839.             if (DoublePage && !SkipMode) {
  840.                if (PassNo == 0) {
  841.                   LastPtobePrinted=count[0];
  842.                   if (!Reverse && (WouldPrint == 0)) {
  843.                       if (count[0] == 0l) {
  844.                          ZeroPage = _TRUE;
  845.                          EvenPage = _FALSE;
  846.                       }
  847.                       else {
  848.                          EvenPage = (bool) ( (count[0]<0? labs(count[0])+1: count[0]) %2 == 0);
  849.  
  850.                          if (PrintEmptyPages && EvenPage && PageParity==1) {
  851.                            WouldPrint ++;
  852.                if (PrintFirstPart) {
  853.                  qfprintf(stderr,"[EvenPage] ");
  854.                  FormFeed();
  855.                }
  856.                          }
  857.                       }
  858.                   }
  859.                   WouldPrint ++;
  860. /*
  861.     fprintf(stderr, "doublepage %d, page parity = %d, 1=%d 2=%d, Reverse %d, WouldPrint %d, fpZ %d\n",
  862.         (int)DoublePage, (int)PageParity,(int)PrintFirstPart,(int)PrintSecondPart,
  863.         (int)Reverse, (int)WouldPrint, (int)ZeroPage);
  864. */
  865.         }
  866.            if (!PrintFirstPart && PageParity==1) { 
  867.          if (count[0]==0l) {
  868.            ZeroPage = _TRUE;
  869.            EvenPage = _FALSE;
  870.          }
  871.          SkipMode = _TRUE;
  872.            }
  873.                else { 
  874. /*
  875. fprintf(stderr,"FirstPart\n count %d, mod %d, pp %d\n",(int)count[0],(int)count[0]%2,PageParity);
  876. */
  877.          SkipMode = (bool)(PageParity != (short)(
  878.                              (count[0]<0?  labs(count[0])+1: count[0])%2));
  879.          if (count[0]==0l) SkipMode=(bool)!SkipMode;
  880.            }
  881.  
  882.             }
  883.             Emitting = (bool)((PassNo != 0) && !SkipMode);
  884. /*
  885. fprintf(stderr,"Emitting= %d, PassNo=%d, SkipMode = %d\n",(int)Emitting,(int)PassNo,(int)SkipMode);
  886. */
  887.             if ( !SkipMode ) {
  888.                 if ( PassNo == 0)
  889.                     qfprintf(stderr, "[%ld", count[0]);
  890.             }
  891.             break;
  892.         case EOP:
  893.             if ( !SkipMode ) {
  894.                 if ( PassNo == 0 ) {
  895.                     /* start second pass on current page */
  896.                     fseek(dvifp, cpagep, 0);
  897.                     PassNo = 1;
  898. #ifdef DEBUG
  899.             if (Debug)
  900.                       fprintf(stderr,"\nStarting second pass\n");
  901. #endif
  902.  
  903.                 } else {
  904.                    /* end of second pass, and of page processing */
  905.  
  906.                     last_ry = UNKNOWN;
  907.                     FormFeed();
  908.                     ++ndone;
  909.  
  910.                     qfprintf(stderr, "] ");
  911.                     if ( (ndone % 10) == 0 )
  912.                           qfprintf(stderr, "\n");
  913.  
  914.                     if (DoublePage) --PrintPages;
  915.                     if (--PrintPages < 1) AllDone(_TRUE);
  916.                     PassNo = 0;
  917.                 }
  918.             } else
  919.                 PassNo = 0;
  920.  
  921.             if ( PassNo == 0 && Reverse ) {
  922.                 if ( ppagep > 0 )
  923.                     fseek(dvifp, ppagep, 0);
  924.                 else {
  925.                    if (DoublePage && !SkipMode)
  926.                      ZeroPage = (bool)(count[0]==0l);
  927.  
  928.                    if (ZeroPage)
  929.                      EvenPage = _FALSE;
  930.                    else
  931.                      EvenPage = (bool) ((int)LastPtobePrinted%2 == 0);
  932.  
  933.                    AllDone(_FALSE);
  934.                 }
  935.             }
  936.             break;
  937.         case PUSH:
  938.             if (sp >= STACK_SIZE)
  939.                 Fatal("stack overflow");
  940.             stack[sp].h = h;
  941.             stack[sp].v = v;
  942.             stack[sp].w = w;
  943.             stack[sp].x = x;
  944.             stack[sp].y = y;
  945.             stack[sp].z = z;
  946.             sp++;
  947.             break;
  948.         case POP:
  949.             --sp;
  950.             if (sp < 0)
  951.                 Fatal("stack underflow");
  952.             h = stack[sp].h;
  953.             v = stack[sp].v;
  954.             w = stack[sp].w;
  955.             x = stack[sp].x;
  956.             y = stack[sp].y;
  957.             z = stack[sp].z;
  958.             break;
  959.         case RIGHT1:
  960.         case RIGHT2:
  961.         case RIGHT3:
  962.         case RIGHT4:
  963.             val = SignExtend(dvifp, (int) command - RIGHT1 + 1);
  964.             if (Emitting)
  965.                 MoveOver(val);
  966.             break;
  967.         case W0:
  968.             if (Emitting)
  969.                 MoveOver(w);
  970.             break;
  971.         case W1:
  972.         case W2:
  973.         case W3:
  974.         case W4:
  975.             w = SignExtend(dvifp, (int)command - W1 + 1);
  976.             if (Emitting)
  977.                 MoveOver(w);
  978.             break;
  979.         case X0:
  980.             if (Emitting)
  981.                 MoveOver(x);
  982.             break;
  983.         case X1:
  984.         case X2:
  985.         case X3:
  986.         case X4:
  987.             x = SignExtend(dvifp, (int)command - X1 + 1);
  988.             if (Emitting)
  989.                 MoveOver(x);
  990.             break;
  991.         case DOWN1:
  992.         case DOWN2:
  993.         case DOWN3:
  994.         case DOWN4:
  995.             val = SignExtend(dvifp, (int)command - DOWN1 + 1);
  996.             if (Emitting)
  997.                 MoveDown(val);
  998.             break;
  999.         case Y0:
  1000.             if (Emitting)
  1001.                 MoveDown(y);
  1002.             break;
  1003.         case Y1:
  1004.         case Y2:
  1005.         case Y3:
  1006.         case Y4:
  1007.             y = SignExtend(dvifp, (int)command - Y1 + 1);
  1008.             if (Emitting)
  1009.                 MoveDown(y);
  1010.             break;
  1011.         case Z0:
  1012.             if (Emitting)
  1013.                 MoveDown(z);
  1014.             break;
  1015.         case Z1:
  1016.         case Z2:
  1017.         case Z3:
  1018.         case Z4:
  1019.             z = SignExtend(dvifp, (int)command - Z1 + 1);
  1020.             if (Emitting)
  1021.                 MoveDown(z);
  1022.             break;
  1023.         case FNT1:
  1024.         case FNT2:
  1025.         case FNT3:
  1026.         case FNT4:
  1027.             if (!SkipMode) {
  1028.                 SetFntNum(NoSignExtend(dvifp, (int)command -FNT1 +1),Emitting);
  1029.             }
  1030.             break;
  1031.         case XXX1:
  1032.         case XXX2:
  1033.         case XXX3:
  1034.         case XXX4:
  1035.             k = (int) NoSignExtend(dvifp, (int)command - XXX1 + 1);
  1036.             GetBytes(dvifp, SpecialStr, k);
  1037.             if (Emitting)
  1038.                 DoSpecial(SpecialStr, k);
  1039.             break;
  1040.         case FNT_DEF1:
  1041.         case FNT_DEF2:
  1042.         case FNT_DEF3:
  1043.         case FNT_DEF4:
  1044.             k = (int) NoSignExtend(dvifp, (int)command - FNT_DEF1 + 1);
  1045.             SkipFontDef();    /* SkipFontDef(k); */
  1046.             break;
  1047.         case PRE:
  1048.             Fatal("PRE occurs within file");
  1049.             break;
  1050.         case POST:
  1051.             AllDone(_FALSE);
  1052.             PassNo = 0;
  1053.             break;
  1054.         case POST_POST:
  1055.             Fatal("POST_POST with no preceding POST");
  1056.             break;
  1057.         default:
  1058.             if (command >= FONT_00 && command <= FONT_63) {
  1059.                 if (!SkipMode)
  1060.                     SetFntNum((long) command - FONT_00, Emitting);
  1061.             } else if (command >= SETC_000 && command <= SETC_127) {
  1062.                 if (!SkipMode) {
  1063.                     SetString(command, PassNo);
  1064.                 }
  1065.             } else
  1066.                 Fatal("%d is an undefined command", command);
  1067.             break;
  1068.         }
  1069.     } /* while _TRUE */
  1070. }
  1071.  
  1072.  
  1073. /*-->ActualFactor*/
  1074. /**********************************************************************/
  1075. /**************************  ActualFactor  ****************************/
  1076. /**********************************************************************/
  1077. double  /* compute the actual size factor given the approximation */
  1078. ActualFactor(unmodsize)
  1079. long    unmodsize;                 /* actually factor * 1000 */
  1080. {
  1081.     double  realsize;     /* the actual magnification factor */
  1082.     realsize = (double)unmodsize / 1000.0;
  1083.     if (abs((int)(unmodsize - 1095l))<2)
  1084.         realsize = 1.095445115; /*stephalf*/
  1085.     else if (abs((int)(unmodsize - 1315l))<2)
  1086.         realsize = 1.31453414; /*stepihalf*/
  1087.     else if (abs((int)(unmodsize - 1577l))<2)
  1088.         realsize = 1.57744097; /*stepiihalf*/
  1089.     else if (abs((int)(unmodsize - 1893l))<2)
  1090.         realsize = 1.89292916; /*stepiiihalf*/
  1091.     else if (abs((int)(unmodsize - 2074l))<2)
  1092.         realsize = 2.0736;   /*stepiv*/
  1093.     else if (abs((int)(unmodsize - 2488l))<2)
  1094.         realsize = 2.48832;  /*stepv*/
  1095.     else if (abs((int)(unmodsize - 2986l))<2)
  1096.         realsize = 2.985984; /*stepvi*/
  1097.     /* the remaining magnification steps are represented with sufficient
  1098.        accuracy already */
  1099.     return(realsize);
  1100. }
  1101.  
  1102.  
  1103. /*-->AllDone*/
  1104. /**********************************************************************/
  1105. /****************************** AllDone  ******************************/
  1106. /**********************************************************************/
  1107. void
  1108. AllDone(PFlag)
  1109. bool PFlag;
  1110. {
  1111. #ifdef TIMING
  1112.     double  time;
  1113. #endif
  1114.  
  1115.     if (DoublePage && (PageParity==1)) { /* Shall we go around again?*/
  1116.         int k;
  1117.         char    n[STRSIZE];
  1118.  
  1119.         if (PrintEmptyPages && EvenPage && Reverse && PrintFirstPart) {
  1120.             WouldPrint ++;
  1121.             qfprintf(stderr,"[EvenPage] ");
  1122.             FormFeed();
  1123.         }
  1124. #ifdef LJ
  1125.         Reverse = (bool)!Reverse;
  1126. #endif
  1127.         if (Reverse) {
  1128.             if (!PFlag) {
  1129.                fseek (dvifp, postambleptr, 0);
  1130.                (void) NoSignExtend(dvifp, 1);
  1131.                ppagep = NoSignExtend(dvifp, 4);
  1132.             }
  1133.             fseek(dvifp, ppagep, 0);
  1134.         } else {
  1135.             fseek(dvifp,  14l, 0);
  1136.         k = (int) NoSignExtend(dvifp, 1);
  1137.         GetBytes(dvifp, n, k);
  1138.         }
  1139.  
  1140.     if (PrintSecondPart) {
  1141.       if (PrintFirstPart) {
  1142.         qfprintf(stderr,"\n----------------------starting second pass\n");
  1143. #ifdef LJ
  1144.         EMIT(outfp, "\033&l2H"); /* Manual Feed */
  1145. #endif
  1146. #ifdef IBM3812
  1147.         PMPout(6,"\326\001\305\300\326\252");
  1148.         /* set display; ring bell; stop; clear display */
  1149.         PMPflush;
  1150. #endif
  1151.       }
  1152.  
  1153.       if (PrintEmptyPages && Reverse) {
  1154.         if (ZeroPage) WouldPrint++;
  1155.         if ((WouldPrint%2) == 1) {
  1156.           qfprintf(stderr,"[Padding] ");
  1157.           FormFeed();
  1158.         }
  1159.       }
  1160.       WouldPrint = 0;
  1161.       if (PrintEmptyPages && !Reverse && ZeroPage) {
  1162.         WouldPrint++;
  1163.         qfprintf(stderr,"[ZeroPage] ");
  1164.         FormFeed();
  1165.       }
  1166.       PageParity = 0;
  1167.       PrintPages = StartPrintPages;
  1168.       return;
  1169.     }
  1170.     }
  1171.  
  1172.     if (EvenPage && DoublePage && !Reverse) WouldPrint++;
  1173.  
  1174.     if (PrintEmptyPages && DoublePage && PrintSecondPart) {
  1175.         if (Reverse) {
  1176.       if (ZeroPage) {
  1177.             WouldPrint ++;
  1178.             qfprintf(stderr,"[ZeroPage] ");
  1179.             FormFeed();
  1180.       }
  1181.         }
  1182.         else if ((WouldPrint%2) != 0) {
  1183.             qfprintf(stderr,"[Padding] ");
  1184.             FormFeed();
  1185.         }
  1186.     }
  1187.  
  1188. #ifdef IBM3812
  1189.     PMPout(10, "\373\010PMP.init");  /* re-init printer  */
  1190.     PMPflush;
  1191.  
  1192.     if (used_fontstorage > MAXFONTSTORAGE) {
  1193.         Warning("\n\7used font_storage of %s: %ld Bytes (of %ld)\7",
  1194.             PRINTER, used_fontstorage, MAXFONTSTORAGE);
  1195.         Warning("Try to format file in separate runs!");
  1196.     } else
  1197.         qfprintf(stderr,
  1198.            "\nAll done, used font_storage of %s: %ld Bytes (of %ld)",
  1199.              PRINTER, used_fontstorage, MAXFONTSTORAGE);
  1200. #endif
  1201. #ifdef LJ
  1202.     qfprintf(stderr, "\nAll done, used font_storage of %s: %ld Bytes",
  1203.              PRINTER, used_fontstorage);
  1204. #ifdef LJ_RESIDENT_FONTS
  1205.     qfprintf(stderr, " + %d resident font%s", resident_count,
  1206.              resident_count == 1 ? "" : "s");
  1207. #endif
  1208.     EMIT(outfp, "\033E");
  1209. #ifdef LJ4
  1210.     EMIT(outfp, "\033%%-12345X");   /* what does it? */
  1211. #endif
  1212.     if (PrintTestPage) EMIT(outfp, "\033z");
  1213. #ifdef vms
  1214.     /* last record is not flushed to file, unless it is completely filled */
  1215.     for (kk = (int)((*outfp)->_cnt); kk > 0; --kk) putc('\0',outfp);
  1216.     fflush(outfp);
  1217. #endif
  1218. #endif
  1219.  
  1220.     if (!G_quiet) {
  1221.         fprintf(stderr,"\nDynamically allocated storage: %ld Bytes \n",
  1222.                     allocated_storage);
  1223.         fprintf(stderr,"%d characters downloaded as soft fonts\n", G_ncdl);
  1224.  
  1225. #ifdef TIMING
  1226. #ifdef BSD_TIME_CALLS
  1227.         ftime(&timebuffer);
  1228.         time = (timebuffer.time + (float)(timebuffer.millitm)/1000.0) 
  1229.            - start_time;
  1230. #else
  1231.     gettimeofday(&Tp, NULL);
  1232.     time = (Tp.tv_sec + (float)(Tp.tv_usec)/1000000.0) - start_time;
  1233. #endif
  1234.  
  1235.         if (ndone > 0) {
  1236.             fprintf(stderr,
  1237.            "Time of complete run: %.2f seconds, %d page(s), %.2f seconds/page",
  1238.                       time, ndone, time / ndone);
  1239.             fprintf(stderr," (%.2f ppm)\n",(ndone * 60) / time);
  1240.         }
  1241.         fprintf(stderr,"\n");
  1242. #endif
  1243.     }
  1244.  
  1245.     exit(G_errenc);
  1246. }
  1247.  
  1248.  
  1249. /*-->CopyFile*/   /* copy a file straight through to output */
  1250. /*********************************************************************/
  1251. /***************************** CopyFile ******************************/
  1252. /*********************************************************************/
  1253. void
  1254. CopyFile( str )
  1255. char    *str;
  1256. {
  1257.     FILE    * spfp;
  1258.     char    t;
  1259.     if ( (spfp = BINOPEN(str)) == NULL ) {
  1260.         Warning("Unable to open file %s", str );
  1261.         return;
  1262.     }
  1263.     qfprintf(stderr," [%s", str);
  1264.     for (t = (char)getc(spfp); !feof(spfp); t = (char)getc(spfp))
  1265.         putc(t, outfp);
  1266.     fclose(spfp);
  1267.     qfprintf(stderr,"]");
  1268. }
  1269.  
  1270.  
  1271. /*-->CopyHPFile*/  /* copy a HP file to output removing unwanted control codes*/
  1272. /*********************************************************************/
  1273. /***************************** CopyHPFile ******************************/
  1274. /*********************************************************************/
  1275. void
  1276. CopyHPFile( str )
  1277. char    *str;
  1278. {
  1279.     FILE    * spfp;
  1280.     char    t,numstr[20];
  1281.     int        count,rx,ry,miny,minx,num;
  1282.     
  1283.     if ( (spfp = BINOPEN(str)) == NULL ) {
  1284.         Warning("Unable to open file %s", str );
  1285.         return;
  1286.     }    
  1287.     minx=32767;            /* Set to a high value initially */
  1288.     miny=32767;
  1289.     
  1290. /* Pass through the input PCL file twice.  The first time find the smallest  */
  1291. /* x- and y-offsets so that they can be subtracted out when sending          */
  1292. /* positioning commands later.   The second pass strips the unwanted commands */
  1293. /* from the input file and outputs the rest */
  1294.     
  1295.     qfprintf(stderr," [%s", str);
  1296.     for (t = (char)getc(spfp); !feof(spfp); t = (char)getc(spfp))
  1297.     {
  1298.       if (t==0x1B)    /* Find the escape character */
  1299.         {
  1300.       t=(char) getc(spfp);
  1301.       if (t==0x2A)     /* This indiactes the start of a graphics command */
  1302.         {                                    
  1303.  
  1304.           t=(char) getc(spfp);
  1305.           switch(t)
  1306.         {
  1307.         case(0x70):
  1308.           /* These are the graphics positioning commands */
  1309.           /* Find the smallest x and y offsets */ 
  1310.           num=0;
  1311.           count=0;
  1312.           for (t=(char) getc(spfp);t<0x40;t=(char) getc(spfp))
  1313.             numstr[count++]=t;
  1314.           numstr[count]=0;
  1315.           num= atoi (numstr);
  1316.           
  1317.           /* Take into account the possible different ordering */
  1318.           /* of the commands (x first, y first) */
  1319.                         
  1320.           if ((t==0x59)||(t==0x79))
  1321.             {
  1322.               if (numstr[0]!='+'&&numstr[0]!='-'&&numstr[0]!='-')
  1323.             if (num<miny) miny=num;
  1324.               if (t==0x79)
  1325.             {
  1326.               num=0;
  1327.               count=0;
  1328.               for (t=(char) getc(spfp);t<0x40;t=(char) getc(spfp))
  1329.                 numstr[count++]=t;
  1330.               numstr[count]=0;
  1331.               num=atoi (numstr);
  1332.               if(numstr[0]!='+'&&numstr[0]!='-'&&numstr[0]!='-')
  1333.                 if (num<minx) minx=num;
  1334.             }
  1335.             }
  1336.           if ((t==0x58)||(t==0x78))
  1337.             {
  1338.               if (numstr[0]!='+'&&numstr[0]!='-'&&numstr[0]!='-')
  1339.             if (num<minx) minx=num;
  1340.               if (t==0x78)
  1341.             {
  1342.               num=0;
  1343.               count=0;
  1344.               for (t=(char) getc(spfp);t<0x40;t=(char) getc(spfp))
  1345.                 numstr[count++]=t;
  1346.               numstr[count]=0;
  1347.               num= atoi (numstr);
  1348.               if (numstr[0]!='+'&&numstr[0]!='-'&&numstr[0]!='-')
  1349.                 if (num<miny) miny=num;
  1350.             }
  1351.             }
  1352.           break;
  1353.           /* Ignore all other commands for the moment - just read them */                            
  1354.         case(0x74):    
  1355.           for (t=(char) getc(spfp);t != 0x52; t=(char) getc(spfp));
  1356.           break;
  1357.           
  1358.         case(0x72):
  1359.           for (t=(char) getc(spfp);((t< 0x40)||(t>0x60)); t=(char) getc(spfp));
  1360.           break;
  1361.  
  1362.         case(0x62):
  1363.           num=0;
  1364.           count=0;
  1365.           /* Read in the correct number of bytes of raster graphics */
  1366.           /* so that we don't get commands and raster graphics confused */
  1367.           
  1368.           for (t=(char) getc(spfp);((t<0x40)||(t>=0x60));t=(char) getc(spfp))
  1369.             numstr[count++]=t;
  1370.           numstr[count]=0;
  1371.           if (t==0x4D)
  1372.             for(t=numstr[count=0];t!=0;t=numstr[++count]);
  1373.           if (t==0x57)
  1374.             {
  1375.               for(t=numstr[count=0];t!=0;t=numstr[++count]);
  1376.               for(count= atoi(numstr);count>0;count--)
  1377.             t=(char) getc(spfp);    
  1378.             }
  1379.           break;
  1380.                                 
  1381.         case(0x63):
  1382.           for (t=(char) getc(spfp);((t< 0x40)||(t>0x60)); t=(char) getc(spfp));
  1383.           break;                                            
  1384.           
  1385.         default:
  1386.           break;
  1387.  
  1388.                     
  1389.         }
  1390.         }
  1391.         }
  1392.     }
  1393.     fclose(spfp);
  1394.     qfprintf(stderr,"]");
  1395.        
  1396.     if ( (spfp = BINOPEN(str)) == NULL ) {
  1397.       Warning("Unable to open file %s", str );
  1398.       return;
  1399.     }
  1400.     qfprintf(stderr," [%s", str);
  1401.  
  1402. /* Pass through the input file again but this time output the */
  1403. /* retained PCL commands */
  1404.  
  1405.     for (t = (char)getc(spfp); !feof(spfp); t = (char)getc(spfp))
  1406.       {
  1407.         if (t==0x1B)
  1408.       {
  1409.         t=(char) getc(spfp);
  1410.         if (t==0x2A)
  1411.           {                                    
  1412.         t=(char) getc(spfp);
  1413.         switch(t)
  1414.           {
  1415.           case(0x70):
  1416.             num=0;
  1417.             count=0;
  1418.             for (t=(char) getc(spfp);t<0x40;t=(char) getc(spfp))
  1419.               numstr[count++]=t;
  1420.             numstr[count]=0;
  1421.             num= atoi (numstr);
  1422.             if ((t==0x59)||(t==0x79))
  1423.               {
  1424.             if (numstr[0]!='+'&&numstr[0]!='-')
  1425.               {
  1426.                 /* Subtract the minimum offset found in first pass */
  1427.                 /* and add in the current vertical offset */
  1428.                 ry=num-miny+(int)PIXROUND(v,vconv)+y_goffset;    
  1429.                 /* Output the new positioning command */
  1430.                 EMIT(outfp,"\033*p%dY",ry);
  1431.               }
  1432.             
  1433.             else if (num>=0) EMIT(outfp,"\033*p%c%dY",numstr[0],num);
  1434.             else EMIT(outfp,"\033*p%dY",num);
  1435.                                 
  1436.             if (t==0x79)
  1437.               {
  1438.                 num=0;
  1439.                 count=0;
  1440.                 for (t=(char) getc(spfp);t<0x40;t=(char) getc(spfp)) 
  1441.                   numstr[count++]=t;   /* reconstructed code */
  1442.                 numstr[count]=0;
  1443.                 num=atoi (numstr);
  1444.                 if(numstr[0]!='+'&&numstr[0]!='-')
  1445.                   {    
  1446.                 /*Add in correct horizontal offset */
  1447.                 rx=num-minx+(int)PIXROUND(h,hconv)+x_goffset;
  1448.                 EMIT(outfp,"\033*p%dX",rx);
  1449.                   }
  1450.                 else if (num>=0) EMIT(outfp,"\033*p%c%dX",numstr[0],num);
  1451.                 else EMIT(outfp,"\033*p%dX",num);
  1452.               }
  1453.               }
  1454.                         
  1455.             if ((t==0x58)||(t==0x78))
  1456.               {
  1457.             if(numstr[0]!='+'&&numstr[0]!='-')
  1458.               {
  1459.                 /*Add in the correct horizontal offset*/
  1460.                 rx=num-minx+(int)PIXROUND(h,hconv)+x_goffset;
  1461.                 EMIT(outfp,"\033*p%dX",rx);
  1462.               }
  1463.             else if (num>=0) EMIT(outfp,"\033*p%c%dX",numstr[0],num);
  1464.             else EMIT(outfp,"\033*p%dX",num);
  1465.                                     
  1466.             if (t==0x78)
  1467.               {
  1468.                 num=0;
  1469.                 count=0;
  1470.                 for (t=(char) getc(spfp);t<0x40;t=(char) getc(spfp))
  1471.                   numstr[count++]=t;
  1472.                 numstr[count]=0;
  1473.                 num= atoi (numstr);
  1474.                 if(numstr[0]!='+'&&numstr[0]!='-')
  1475.                   {
  1476.                 /*Add in correct vertical offset*/
  1477.                 ry=num-miny+(int)PIXROUND(v,vconv)+y_goffset;
  1478.                 EMIT(outfp,"\033*p%dY",ry);
  1479.                   }
  1480.                 else if (num >=0) EMIT(outfp,"\033*p%c%dY",numstr[0],num);
  1481.                 else EMIT(outfp,"\033*p%dY",num);
  1482.                                }
  1483.               }
  1484.             break;
  1485.                                 
  1486.           case(0x74):
  1487.             /* Set the Raster resolution */
  1488.             EMIT(outfp,"\033*t");
  1489.             for (t=(char) getc(spfp);t != 0x52; t=(char) getc(spfp))
  1490.               putc(t,outfp);
  1491.             putc(t,outfp);
  1492.             break;
  1493.                                
  1494.           case(0x72):
  1495.             /* Raster Graphics commands such as start */
  1496.             EMIT(outfp,"\033*r");
  1497.             for (t=(char) getc(spfp);((t< 0x40)||(t>0x60)); t=(char) getc(spfp))
  1498.               putc(t,outfp);
  1499.             putc(t,outfp);
  1500.             break;
  1501.  
  1502.           case(0x62):
  1503.             /* Transfer the correct number of bytes of raster graphics */
  1504.             EMIT(outfp,"\033*b");
  1505.             num=0;
  1506.             count=0;
  1507.             for (t=(char) getc(spfp);((t<0x40)||(t>=0x60));t=(char) getc(spfp))
  1508.               numstr[count++]=t;
  1509.             numstr[count]=0;
  1510.             if (t==0x4D)
  1511.               {
  1512.             for(t=numstr[count=0];t!=0;t=numstr[++count])
  1513.               putc(t,outfp);
  1514.             EMIT(outfp,"M");
  1515.               }
  1516.             if (t==0x57)
  1517.               {
  1518.             for(t=numstr[count=0];t!=0;t=numstr[++count])
  1519.               putc(t,outfp);
  1520.             EMIT(outfp,"W");
  1521.             for(count= atoi(numstr);count>0;count--)
  1522.               {
  1523.                 t=(char) getc(spfp);
  1524.                 putc(t,outfp);
  1525.               }    
  1526.               }
  1527.             break;
  1528.                                     
  1529.           case(0x63):
  1530.             /* Rectangular draw commands */
  1531.             EMIT(outfp,"\033*c");
  1532.             for (t=(char) getc(spfp);((t< 0x40)||(t>0x60)); t=(char) getc(spfp))
  1533.               putc(t,outfp);
  1534.             putc(t,outfp);
  1535.             break;                                            
  1536.                                        
  1537.           default:
  1538.             break;
  1539.  
  1540.                     
  1541.           }
  1542.           }
  1543.       }
  1544.       }
  1545.     fclose(spfp);
  1546.     qfprintf(stderr,"]");
  1547. }
  1548.  
  1549.  
  1550.  
  1551. /*-->DecodeArgs*/
  1552. /*********************************************************************/
  1553. /***************************** DecodeArgs ****************************/
  1554. /*********************************************************************/
  1555. void
  1556. DecodeArgs( argc, argv )
  1557. int     argc;
  1558. char    *argv[];
  1559. {
  1560.     int     argind;            /* argument index for flags      */
  1561.     char    curarea[STRSIZE];  /* current file area             */
  1562.     char    curname[STRSIZE];  /* current file name             */
  1563.     char    *tcp, *tcp1;       /* temporary character pointers  */
  1564.     char    *this_arg;
  1565.     double  x_offset=0.0, y_offset=0.0;
  1566.  
  1567. #ifndef KPATHSEA
  1568.     if ((tcp = getenv("TEXPXL")) != NULL) PXLpath = tcp;
  1569. #ifdef LJ_RESIDENT_FONTS
  1570.     if ((tcp = getenv("TFMFONTS")) != NULL) 
  1571.       TFMpath = tcp;
  1572.     else if ((tcp = getenv("TEXFONTS")) != NULL) 
  1573.       TFMpath = tcp;
  1574. #endif
  1575. #endif
  1576.  
  1577.     argind = 1;
  1578.     while (argind < argc) {
  1579.         tcp = argv[argind];
  1580.         if ((tcp[0] == '-') && (tcp[1] != '\0')) {
  1581.             ++tcp;
  1582.             switch (*tcp) {
  1583. #ifndef KPATHSEA
  1584.             case 'a':       /* a selects different pxl font area */
  1585.                 PXLpath = ++tcp;
  1586.                 break;
  1587. #endif
  1588. #ifdef IBM3812
  1589.             case 'b':       /* first page from alternate casette */
  1590.                 FirstAlternate = _TRUE;
  1591.                 break;
  1592. #endif
  1593.             case 'c':       /* number of copies to print */
  1594.                 if ( sscanf(tcp + 1, "%hd", &ncopies) != 1 )
  1595.                    Fatal("Argument of -c is not a valid integer\n");
  1596.                 if (ncopies<1) {
  1597.                   Warning("argument of -c < 1; set to 1!");
  1598.                   ncopies=1;
  1599.                 }
  1600.                 break;
  1601. #ifdef DEBUG
  1602.             case '-':       /* --D selects Debug output */
  1603.         tcp++;
  1604.         if (*tcp == 'D')
  1605.           Debug = _TRUE;
  1606.                 break;
  1607. #endif
  1608. #ifdef LJ2P
  1609.             case 'd':       /* d selects DUPLEX mode  */
  1610.         tcp++;
  1611.         if (*tcp == '1' ) DuplexMode = 1;
  1612.         else if (*tcp == '2') DuplexMode = 2;
  1613.                 else {
  1614.           Warning("Invalid DUPLEX mode, assuming DUPLEX=1, Long-Edge Binding");
  1615.           DuplexMode = 1;
  1616.         }
  1617.                 break;
  1618. #endif
  1619.             case 'D':       /* D selects DoublePage  */
  1620.                 DoublePage = _TRUE;
  1621.         tcp++;
  1622.         if (*tcp == '1' || *tcp == '2') {
  1623.           if (*tcp == '2') PrintFirstPart = _FALSE;
  1624.           else             PrintSecondPart = _FALSE;
  1625.           tcp++;
  1626.         }
  1627.         if (*tcp == '-') PrintEmptyPages = _FALSE;
  1628.                 break;
  1629. #ifdef LJ4
  1630.             case 'E':       /* do not reset printer (go) */
  1631.                 econoMode = _TRUE;
  1632.                 break;
  1633. #endif
  1634.             case 'e':       /* emit file is specified */
  1635.                 EmitFileName = ++tcp;
  1636. #ifdef MSDOS
  1637.                 /* delete trailing ':' (causing hangup) */
  1638.                 if (EmitFileName[strlen(EmitFileName)-1] == ':')
  1639.                     EmitFileName[strlen(EmitFileName)-1] = '\0';
  1640. #endif
  1641. #ifdef OS2  /* repeated to avoid problems with stupid c preprocessors  */
  1642.                 /* delete trailing ':' (causing hangup) */
  1643.                 if (EmitFileName[strlen(EmitFileName)-1] == ':')
  1644.                     EmitFileName[strlen(EmitFileName)-1] = '\0';
  1645. #endif
  1646.                 break;
  1647.             case 'f':       /* next arg is starting pagenumber */
  1648.                 if ( sscanf(tcp + 1, "%ld", &FirstPage) != 1 )
  1649.                     Fatal("Argument is not a valid integer\n");
  1650.                 FirstPageSpecified = _TRUE;
  1651.                 break;
  1652. #ifdef LJ
  1653.             case 'g':       /* do not reset printer (go) */
  1654.                 ResetPrinter = _FALSE;
  1655.                 break;
  1656. #endif
  1657.             case 'h':     /* copy header file through to output  */
  1658.                 HeaderFileName = ++tcp;
  1659.                 G_header = _TRUE;
  1660.                 break;
  1661. #if defined(LJ2P) || defined(IBM3812)
  1662.             case 'l':       /* landscape  */
  1663.                 Landscape = _TRUE;
  1664.                 break;
  1665. #endif
  1666. #ifdef MAKETEXPK
  1667.         case 'M':
  1668.         /* -M, -M1 => don't make font; -M0 => do.  */
  1669.             makeTexPK = *(tcp + 1) == '0';
  1670.             break;
  1671. #endif
  1672.             case 'x':       /* specify x-offset */
  1673.                 this_arg = 0;
  1674.                 if (!(*++tcp)) {
  1675.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1676.                 } else {
  1677.                     this_arg = tcp;
  1678.                 }
  1679.                 if (!this_arg
  1680.                      || sscanf(this_arg,"%lf", &x_offset) != 1)
  1681.          Fatal("Argument of -x is not a valid floating point number\n");
  1682.                 break;
  1683.             case 'y':       /* specify y-offset */
  1684.                 this_arg = 0;
  1685.                 if (!(*++tcp)) {
  1686.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1687.                 } else {
  1688.                     this_arg = tcp;
  1689.                 }
  1690.                 if (!this_arg || sscanf(this_arg, "%lf", &y_offset) != 1)
  1691.                 Fatal("Argument of -y is not a valid floating point number\n");
  1692.                 break;
  1693.             case 'X':       /* specify X-origin in dots */
  1694.                 this_arg = 0;
  1695.                 if (!(*++tcp)) {
  1696.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1697.                 } else {
  1698.                     this_arg = tcp;
  1699.                 }
  1700.                 if (!this_arg || sscanf(this_arg,"%hd", &x_origin) != 1)
  1701.                    Fatal("Argument of -X is not a valid integer\n");
  1702.                 break;
  1703.             case 'Y':       /* specify Y-origin in dots */
  1704.                 this_arg = 0;
  1705.                 if (!(*++tcp)) {
  1706.                     this_arg = (++argind >= argc ? 0 : argv[argind]);
  1707.                 } else {
  1708.                     this_arg = tcp;
  1709.                 }
  1710.                 if (!this_arg ||
  1711.                      sscanf(this_arg, "%hd", &y_origin) != 1)
  1712.                         Fatal("Argument of -Y is not a valid integer\n");
  1713.                 break;
  1714.             case 'm':       /* specify magnification to use */
  1715.                 switch ( (*++tcp) ) {
  1716.                 case '#':
  1717.                      /* next arg is a magnification to use */
  1718.                     if ( sscanf(tcp + 1, "%ld", &usermag) != 1 )
  1719.                        Fatal("Argument of mag is not a valid integer\n");
  1720.                     break;
  1721.                 case '0':
  1722.                     usermag = 1000;
  1723.                     break;
  1724.                 case 'h':
  1725.                 case 'H':
  1726.                     usermag = 1095;
  1727.                     break;
  1728.                 case '1':
  1729.                     usermag = 1200;
  1730.                     break;
  1731.                 case 'q':
  1732.                     usermag = 1250;
  1733.                     break;
  1734.                 case '2':
  1735.                     usermag = 1440;
  1736.                     break;
  1737.                 case '3':
  1738.                     usermag = 1728;
  1739.                     break;
  1740.                 case '4':
  1741.                     usermag = 2074;
  1742.                     break;
  1743.                 case '5':
  1744.                     usermag = 2488;
  1745.                     break;
  1746.                 default:
  1747.                     Fatal("%c is a bad mag step\n", *tcp);
  1748.                 }
  1749.                 break;
  1750. #ifdef SUPERCOMMENT
  1751.             case 'o':     /* PostScript command to send */
  1752.                 if ( ++argind >= argc )
  1753.                     Fatal("No argument following -o\n", 0);
  1754.                 PScmd[nps++] = argv[argind];
  1755.                 break;
  1756. #endif
  1757.             case 'p':       /* print n pages  */
  1758.                 if ( sscanf(tcp + 1, "%ld", &PrintPages) != 1 )
  1759.                     Fatal("Argument is not a valid integer\n");
  1760.                 if (PrintPages < 1)
  1761.                   Fatal("Argument of -p must be greater than 0\n");
  1762.                 break;
  1763.             case 'q':       /* quiet operation */
  1764.                 G_quiet = _TRUE;
  1765.                 break;
  1766.             case 'r':       /* switch order to process pages */
  1767.                 Reverse = (bool)(!Reverse);
  1768.                 break;
  1769. #ifdef LJ
  1770.             case 's':       /* specify X-origin in dots */
  1771.                 this_arg = 0;
  1772.                 if (!(*++tcp)) this_arg = (++argind >= argc ? 0 : argv[argind]);
  1773.                 else           this_arg = tcp;
  1774.                 if (!this_arg || sscanf(this_arg,"%hd", &pagesize) != 1)
  1775.                    Fatal("Argument of -s is not a valid integer\n");
  1776. /*
  1777.  * The original pgsiz_dots assumed  a resolution of 300dpi. This loses
  1778.  * at 600dpi so we must scale all.
  1779.  */
  1780.                 switch (pagesize) {
  1781.                 case 1: pgsiz_dots = 3150; break;         /* executive */
  1782.                 case 2: pgsiz_dots = 3300; break;         /* letter */
  1783.                 case 3: pgsiz_dots = 4200; break;         /* legal */
  1784.                 case 26: pgsiz_dots = 3507; break;        /* a4 */
  1785.                 case 80: pgsiz_dots = 2250; break;        /* monarc */
  1786.                 case 81: pgsiz_dots = 2850; break;        /* com10 */
  1787.                 case 90: pgsiz_dots = 2598; break;        /* int dl */
  1788.                 case 91: pgsiz_dots = 2704; break;        /* int c5 */
  1789.                 default: Fatal(
  1790. #ifndef vms
  1791.                    "%hd is a bad value for pagesize (1,2,3,26,80,81,90,91)",
  1792. #else
  1793.                    "%d is a bad value for pagesize (1,2,3,26,80,81,90,91)",
  1794. #endif
  1795.                    pagesize);
  1796.                 }
  1797.                 break;
  1798. #endif
  1799.             case 't':       /* ending pagenumber */
  1800.                 if ( sscanf(tcp + 1, "%ld", &LastPage) != 1 )
  1801.                     Fatal("Argument is not a valid integer\n");
  1802.                 LastPageSpecified = _TRUE;
  1803.                 break;
  1804.             case 'v':    /* verbatim mode (print pxl-file names) */
  1805.                 G_noverbatim = _FALSE;
  1806.                 break;
  1807.             case 'w':       /* don't print out warnings */
  1808.                 G_nowarn = _TRUE;
  1809.                 break;
  1810. #ifdef LJ
  1811.             case 'z':
  1812.                 PrintTestPage = (bool)(!PrintTestPage);
  1813.                 break;
  1814. #endif
  1815. #ifdef LJ4
  1816.             case 'R':       /* number of copies to print */
  1817.                 if ( sscanf(tcp + 1, "%d", &RESOLUTION) != 1 )
  1818.                    Fatal("Argument of -R is not a valid integer\n");
  1819.                 if (RESOLUTION != 300 && RESOLUTION != 600 ) {
  1820.                   Warning("Resolution must be 300 or 600! Assuming 300.dpi.");
  1821.                   RESOLUTION=300;
  1822.                 } else {
  1823.           if (RESOLUTION == 600) {
  1824.             MFMODE = MFMODE600;
  1825.             x_origin = 600;
  1826.             y_origin = 600;
  1827.           }
  1828.         }
  1829.                 break;
  1830. #endif
  1831.             default:
  1832.                 fprintf(stderr, "%c is not a legal flag\n", *tcp);
  1833.             }
  1834.         } else {
  1835.  
  1836.             (void) strcpy(filename, tcp);
  1837.         if(!strcmp(filename, "-")) {
  1838.            EmitFileName="-";
  1839.           dvifp=stdin;
  1840.         } else {
  1841.           tcp = strrchr(argv[argind], '/');
  1842.           /* split into directory + file name */
  1843.           if (tcp == NULL)  {
  1844.                 curarea[0] = '\0';
  1845.                 tcp = argv[argind];
  1846.           } else {
  1847.                 (void) strcpy(curarea, argv[argind]);
  1848.                 curarea[tcp-argv[argind]+1] = '\0';
  1849.                 tcp += 1;
  1850.           }
  1851.  
  1852.           (void) strcpy(curname, tcp);
  1853.           /* split into file name + extension */
  1854.           tcp1 = strrchr(tcp, '.');
  1855.           if (tcp1 == NULL) {
  1856.                 (void) strcpy(rootname, curname);
  1857.                 strcat(curname, ".dvi");
  1858.           } else {
  1859.                 *tcp1 = '\0';
  1860.                 (void) strcpy(rootname, curname);
  1861.                 *tcp1 = '.';
  1862.           }
  1863.  
  1864.           (void) strcpy(filename, curarea);
  1865.           (void) strcat(filename, curname);
  1866.  
  1867.           if ((dvifp = BINOPEN(filename)) == NULL)  {
  1868.         /* do not insist on .dvi */
  1869.         if (tcp1 == NULL)  {
  1870.           curname[strlen (curname) - 4] = '\0';
  1871.           filename[strlen (filename) - 4] = '\0';
  1872.         }
  1873.         if (tcp1 != NULL || (dvifp = BINOPEN(filename)) == NULL)  {
  1874.           Fatal("%s: can't find DVI file \"%s\"\n\n",
  1875.             G_progname, filename);
  1876.         }
  1877.           }
  1878.         } /* dvi filename != '-" */
  1879.         }
  1880.         argind++;
  1881.     }
  1882.  
  1883. #ifdef LJ4
  1884.     pgsiz_dots *= (int)(RESOLUTION/300); /* rescale dots to page */
  1885. #endif    
  1886.     x_goffset = (short) MM_TO_PXL(x_offset) + x_origin;
  1887.     y_goffset = (short) MM_TO_PXL(y_offset) + y_origin;
  1888.  
  1889.     if (dvifp == NULL)  {
  1890.       fprintf(stderr,"\nThis is the DVI to %s converter version %s",
  1891.           PRINTER, VERSION);
  1892. #ifdef SEVENBIT
  1893.       fprintf(stderr,", 7bit");
  1894. #endif
  1895.       fprintf(stderr," (%s)\n", OS);
  1896.       fprintf(stderr,"usage: %s [OPTIONS] dvifile\n", G_progname);
  1897.  
  1898.       fprintf(stderr,"OPTIONS are:\n");
  1899. #ifdef DEBUG
  1900.       fprintf(stderr,"\t--D ..... turns debug output on\n");
  1901. #endif
  1902.       fprintf(stderr,
  1903.         "\t-aX ..... X= searchpath leading to pixel-files (.pk or .pxl)\n");
  1904. #ifdef IBM3812
  1905.       fprintf(stderr,
  1906.         "\t-b  ..... take paper for first page from alternate casette\n");
  1907. #endif
  1908.       fprintf(stderr,"\t-cX ..... X= number of copies\n");
  1909. #ifdef LJ2P
  1910.       fprintf(stderr,"\t-dX ..... duplex, X=1: long-edge, 2: short-edge binding\n");
  1911. #endif
  1912.       fprintf(stderr,"\t-D  ..... turns doublepage output on; ");
  1913.       fprintf(stderr,"-D1 odd pages only, -D2 even\n");
  1914. #ifdef LJ4
  1915.       fprintf(stderr,"\t-E  ..... print in econo-mode\n");
  1916. #endif
  1917.       fprintf(stderr,"\t-eX ..... X= output file\n");
  1918.       fprintf(stderr,"\t-fX ..... print from begin of page number X\n");
  1919. #ifdef LJ
  1920.       fprintf(stderr,
  1921.           "\t-g  ..... do not reset printer at begin of job (go)\n");
  1922. #endif
  1923.       fprintf(stderr,"\t-hX ..... X= name of headerfile\n");
  1924. #ifdef LJ2P
  1925.       fprintf(stderr,"\t-l  ..... landscape mode\n");
  1926. #endif
  1927. #ifdef MAKETEXPK
  1928.       fprintf(stderr,"\t-MX ..... Don't generate missing PK files\n");
  1929. #endif
  1930.       fprintf(stderr,"\t-mX ..... magnification X=0;h;1;2;3;4;5;#xxxx\n");
  1931.       fprintf(stderr,"\t-pX ..... print X pages\n");
  1932.       fprintf(stderr,"\t-q  ..... quiet operation\n");
  1933.       fprintf(stderr,"\t-r  ..... process pages in reverse order\n");
  1934. #ifdef LJ4
  1935.       fprintf(stderr,"\t-RX ..... set resolution to 300 or 600 dpi\n");
  1936. #endif
  1937. #ifdef LJ
  1938.       fprintf(stderr,"\t-sX ..... set paper size to X (see documentation)\n");
  1939. #endif
  1940.       fprintf(stderr,"\t-tX ..... print to end of page number X\n");
  1941.       fprintf(stderr,"\t-w  ..... don't print out warnings\n");
  1942.       fprintf(stderr,"\t-v  ..... tell user which pixel-files are used\n");
  1943.       fprintf(stderr,"\t-xX ..... X= x-offset on printout in mm\n");
  1944.       fprintf(stderr,"\t-yX ..... X= y-offset on printout in mm\n");
  1945.       fprintf(stderr,"\t-XO ..... O= x page origin in dots (default=%d)\n",
  1946.             XDEFAULTOFF );
  1947.       fprintf(stderr,"\t-YO ..... O= y page origin in dots (default=%d)\n",
  1948.             YDEFAULTOFF );
  1949. #ifdef LJ
  1950.       fprintf(stderr,
  1951.           "\t-z  ..... print test page with pagecounter after job\n");
  1952. #endif
  1953.       fprintf(stderr,
  1954.       "\t-   ..... dvifile is stdin (must be seekable); implies -e-\n");
  1955.       exit(1);
  1956.     }
  1957.     if (EQ(EmitFileName, "")) {
  1958.         if ((EmitFileName = (char *)malloc( STRSIZE )) != NULL)
  1959.             allocated_storage += STRSIZE;
  1960.         else
  1961.             Fatal("Can't allocate storage of %d bytes\n",STRSIZE);
  1962.         (void) strcpy(EmitFileName, curname);
  1963.         if ((tcp1 = strrchr(EmitFileName, '.'))) 
  1964.       *tcp1 = '\0';
  1965.     strcat(EmitFileName, EMITFILE_EXTENSION);
  1966.     }
  1967. }
  1968.  
  1969.  
  1970. /*-->DoConv*/
  1971. /*********************************************************************/
  1972. /********************************  DoConv  ***************************/
  1973. /*********************************************************************/
  1974. long
  1975. DoConv(num, den, convResolution)
  1976. long    num, den;
  1977. int     convResolution;
  1978. {
  1979.     /*register*/ double conv;
  1980.     conv = ((double)num / (double)den) *
  1981.             ((double)mag / 1000.0) *
  1982.           ((double)convResolution/254000.0);
  1983.  
  1984.     return((long) ((1.0/conv)+0.5));
  1985. }
  1986.  
  1987. /*-->DoSpecial */
  1988. /*********************************************************************/
  1989. /*****************************  DoSpecial  ***************************/
  1990. /*********************************************************************/
  1991. typedef enum  { None, String, Integer, Number, Dimension }
  1992.  
  1993. ValTyp;
  1994. typedef struct {
  1995.     char    *Key;       /* the keyword string */
  1996.     char    *Val;       /* the value string */
  1997.     ValTyp  vt;         /* the value type */
  1998.     union {         /* the decoded value */
  1999.         int     i;
  2000.         float   n;
  2001.     } v;
  2002. } KeyWord;
  2003. typedef struct {
  2004.     char    *Entry;
  2005.     ValTyp  Typ;
  2006. } KeyDesc;
  2007. #define PSFILE 0
  2008. #define ORIENTATION 1
  2009. #define RESETPOINTS 2
  2010. #define DEFPOINT 3
  2011. #define FILL 4
  2012. #define GRAY 5
  2013. #define PATTERN 6
  2014. #define HPFILE 7
  2015. /*--- Code by PAH */
  2016. #define IFFFILE 8
  2017. #define HSCALE 9
  2018. #define VSCALE 10
  2019. #define HOFFSET 11
  2020. #define VOFFSET 12
  2021. #define HSIZE 13
  2022. #define VSIZE 14
  2023. #define ANGLE 15
  2024. /*--- End code by PAH */
  2025. KeyDesc KeyTab[] = {
  2026. /*--- Code by PAH 
  2027.     { "file", String }, */
  2028.     { "psfile", String },  
  2029. /*--- End code by PAH */
  2030.     { "orientation", Integer},
  2031.     { "resetpoints", String},
  2032.     { "defpoint", String},
  2033.     { "fill", String},
  2034.     { "gray", Integer},
  2035.     { "pattern", Integer},
  2036.     { "hpfile", String}
  2037. /*--- Code by PAH */
  2038.     ,{ "ifffile", String},
  2039.      {"hscale", Number},
  2040.      {"vscale", Number},
  2041.      {"hoffset", Dimension},
  2042.      {"voffset", Dimension},
  2043.      {"hsize", Dimension},
  2044.      {"vsize", Dimension},
  2045.      {"angle", Dimension}
  2046. /*--- End code by PAH */
  2047.  
  2048. };
  2049.  
  2050.  
  2051. #define NKEYS (sizeof(KeyTab)/sizeof(KeyTab[0]))
  2052. void
  2053. DoSpecial( str, n )
  2054. /* interpret a \special command, made up of keyword=value pairs */
  2055. char    *str;
  2056. int     n;
  2057. {
  2058.   char    spbuf[STRSIZE];
  2059.   char    hscale[8], vscale[8], hsize[8], vsize[8], hoffset[8], voffset[8];
  2060.   char    angle[8];
  2061.   char    *sf = NULL;
  2062.   KeyWord k;
  2063.   int     i, itype=0;
  2064.  
  2065.   str[n]    = '\0';
  2066.   spbuf[0]  = '\0';
  2067.  
  2068.   hscale[0] ='\0';
  2069.   vscale[0] ='\0';
  2070.   hsize[0]  ='\0';
  2071.   vsize[0]  ='\0';
  2072.   hoffset[0]='\0';
  2073.   voffset[0]='\0';
  2074.   angle[0]  ='\0';
  2075.  
  2076.   SetPosn(h, v);
  2077.   while ( (str = GetKeyStr(str, &k)) != NULL ) {
  2078.     /* get all keyword-value pairs */
  2079.     /* for compatibility, single words are taken as file names */
  2080.     if ( k.vt == None && access(k.Key, 0) == 0) {
  2081.         if ( sf )
  2082.             Warning("More than one \\special file name given. %s ignored", sf);
  2083.         (void) strcpy(spbuf, k.Key);
  2084.         sf = spbuf;
  2085. /*
  2086.         for (j = 1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++);
  2087. */
  2088.     } else if ( GetKeyVal( &k, KeyTab, NKEYS, &i ) && i != -1 )
  2089.         switch (i) {
  2090. /*--- Code by PAH ----------------------------------------------------*/
  2091.         case PSFILE:
  2092.           if ( sf )
  2093.              Warning(
  2094.                "More than one \\special file name given. %s ignored",
  2095.                sf);
  2096.           (void) strcpy(spbuf, k.Val);
  2097.           sf    = spbuf;
  2098.           itype = 1; 
  2099.           break;
  2100.        case IFFFILE:
  2101.           if ( sf )
  2102.              Warning(
  2103.                "More than one \\special file name given. %s ignored",
  2104.                sf);
  2105.           (void) strcpy(spbuf, k.Val);
  2106.           sf    = spbuf;
  2107.           itype = 2;
  2108.           break;
  2109.         case HSCALE: 
  2110.           (void) strcpy(hscale, k.Val);
  2111.            break; 
  2112.         case VSCALE: 
  2113.           (void) strcpy(vscale, k.Val);
  2114.           break;
  2115.         case HSIZE: 
  2116.           (void) strcpy(hsize, k.Val);
  2117.            break; 
  2118.         case VSIZE: 
  2119.           (void) strcpy(vsize, k.Val);
  2120.           break;
  2121.        case HOFFSET:
  2122.           (void) strcpy(hoffset, k.Val);
  2123.           break;
  2124.         case VOFFSET:
  2125.           (void) strcpy(voffset, k.Val);
  2126.           break;
  2127.         case ANGLE:
  2128.           (void) strcpy(angle, k.Val);
  2129.           break;
  2130. /*--- End code by PAH -------------------------------------------------*/
  2131.         case HPFILE:
  2132.             if ( sf )
  2133.                 Warning("More than one \\special file name given. %s ignored",
  2134.                             sf);
  2135.             (void) strcpy(spbuf, k.Val);
  2136.             sf = spbuf;
  2137. /*
  2138.             for (j=1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++) ;
  2139. */
  2140.             break;
  2141. /*------------------------------*/
  2142.         case ORIENTATION:
  2143. #ifdef IBM3812
  2144.             if ((k.v.i >= 0) && (k.v.i < 4)) {
  2145.                 last_ry = UNKNOWN;
  2146.                 sprintf(PMPformat, "\322%c", (unsigned char)k.v.i);
  2147.                 PMPout(2, PMPformat);
  2148.         if (k.v.i == 1) Landscape = _TRUE;
  2149.         else if (k.v.i == 0) Landscape = _FALSE;
  2150. #endif
  2151. #ifdef LJ
  2152.             if ((k.v.i >= 0) && (k.v.i < 2)) {
  2153.                 last_ry = UNKNOWN; 
  2154.                 EMIT(outfp, "\033&l%dO\033*rF", (unsigned char)k.v.i);
  2155. #endif
  2156.             } else
  2157.                 Warning( "Invalid orientation (%d)given; ignored.", k.v.i);
  2158.             break;
  2159. /*------------------------------*/
  2160.         default:
  2161.             Warning("Can't handle %s=%s command; ignored.", k.Key, k.Val);
  2162.             break;
  2163. /*------------------------------*/
  2164.         }
  2165.     else
  2166.         Warning("Invalid keyword or value in \\special - <%s> ignored", k.Key);
  2167.   }
  2168.   if ( sf ) {
  2169.     last_ry = UNKNOWN; 
  2170. #ifdef IBM3812
  2171.     PMPflush;
  2172. #endif
  2173. /*--- Code by PAH ---------------------------------------------*/
  2174. #ifdef AMIGA
  2175.     if (itype>0 )
  2176.     {
  2177.        switch( itype )
  2178.        {  case 1:
  2179.              CopyPSFile(spbuf,hscale,vscale,hoffset,voffset,
  2180.                 hsize,vsize,angle);
  2181.              break;
  2182.           case 2:
  2183.              CopyIFFFile(spbuf,hsize,vsize,hoffset,voffset);
  2184.              break;
  2185.        }
  2186.     }
  2187. #endif
  2188.     else
  2189.     { if (i==HPFILE)  
  2190.          CopyHPFile( sf );
  2191.       else
  2192.          CopyFile( sf );
  2193.     } 
  2194.   }
  2195. }
  2196.  
  2197. /*-->EmitChar*/
  2198. /**********************************************************************/
  2199. /****************************  EmitChar  ******************************/
  2200. /**********************************************************************/
  2201. void                     /* output a character bitmap */
  2202. EmitChar(c, ce)
  2203. long    c;
  2204. struct char_entry *ce;
  2205. {
  2206.     register int    i;
  2207.     register unsigned char  *sl;
  2208.     unsigned short  nbpl, nwpl;
  2209.     long    total;
  2210. #ifdef LJ
  2211.     unsigned char cnv_buffer[10];
  2212. #endif
  2213.  
  2214.   
  2215. #ifdef LJ
  2216. /*
  2217. printf("Emit character %c(%d) id=%d, yoff=%d[%d], w=%d[%d], h=%d[%d]\n",
  2218.         (char)c,(int)c,
  2219.     fontptr->plusid,
  2220.     ce->yOffset, fontptr->max_yoff,
  2221.     ce->width,   fontptr->max_width,
  2222.     ce->height,  fontptr->max_height
  2223. );
  2224. */
  2225. #endif
  2226.  
  2227.  
  2228.  
  2229.     if ( fontptr->ncdl == 0 ) {
  2230. #ifdef IBM3812
  2231.         used_fontstorage += 1028;
  2232. #endif
  2233. #ifdef LJ
  2234.         if (fontptr->max_width == 0) { /* we have no realistic values */
  2235.                 fontptr->max_yoff = CHAR_HEIGTH_LARGE;
  2236.                 fontptr->max_width = CHAR_WIDTH_LARGE;
  2237.                 fontptr->max_height = CHAR_HEIGTH_LARGE*2;
  2238.         }
  2239.  
  2240.         /* open font dict before first char, set active font */
  2241.         INT_ASCII(cnv_buffer,fontptr->plusid);
  2242. #ifdef LJ4
  2243.         EMIT(outfp, "\033*c%sD\033)s68W", cnv_buffer);
  2244.         EMITB(6, "\0\104\024\2\0\0");   
  2245. #else
  2246.         EMIT(outfp, "\033*c%sD\033)s26W", cnv_buffer);
  2247. #ifdef SEVENBIT
  2248.         EMITB(6, "\0\032\0\1\0\0");   
  2249. #else
  2250.         EMITB(6, "\0\032\0\2\0\0");   
  2251. #endif
  2252. #endif /* LJ 4 */
  2253.         EMITWORD( fontptr->max_yoff);
  2254.         EMITWORD( fontptr->max_width);
  2255.         EMITWORD( fontptr->max_height);
  2256.         EMITB(8, "\0\1\1\25\0\4\0\4");
  2257.         EMITB(6, "\0\0\0\0\0\0");
  2258. #ifdef LJ4
  2259.     EMITB(22,"\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\0\0\0\0\0");
  2260.     EMITB(16,"                ");
  2261.     EMITB(4 ,"\2\130\2\130"); 
  2262. #endif
  2263.         EMIT(outfp, "\033*c4F");
  2264. #endif
  2265.     }
  2266.     if ( fontptr != prevfont ) {   /* because this isn't done on pass 0 */
  2267. #ifdef LJ
  2268.         INT_ASCII(cnv_buffer,fontptr->plusid);
  2269.         EMIT(outfp, "\033(%sX", cnv_buffer);
  2270. #endif
  2271.         prevfont = fontptr;
  2272.     }
  2273.  
  2274. #ifdef USEPXL
  2275.     if (fontptr->id == pk89)   {
  2276.         nwpl = 0; /* initialize variable */
  2277.         nbpl = ((int)(ce->width) +  7) >> 3;
  2278.         total = (long)ce->height * nbpl;
  2279.     } else if (fontptr->id == id1002)   {
  2280.         nwpl = 0; /* initialize variable */
  2281.         nbpl = ((int)(ce->width) +  7) >> 3;
  2282.         total = (long)ce->height * nbpl;
  2283.     } else if (fontptr->id == id1001) {
  2284.         nwpl = ((int)(ce->width) + 31) >> 5;
  2285.         nbpl = ((int)(ce->width) + 7) >> 3;
  2286.         total = (long)ce->height * nbpl;
  2287.     } else {
  2288.       /* should never be necessary */
  2289.       nwpl = 0;
  2290.       nbpl = 0;
  2291.       total = 0;
  2292.     }
  2293. #else
  2294.     nbpl = (num_cols + 7) >> 3;
  2295.     total = num_rows * nbpl;
  2296. #endif
  2297. /***************************************************************
  2298.     if ( ((char) c == 'i') && (fontptr->plusid == 0)) {
  2299.         long j;
  2300.         fprintf(stderr, "cols=%ld, ncols=%ld\n",nwpl,nbpl);
  2301.  
  2302.         fprintf(stderr, "%ld Bytes:->",total);
  2303.         for (j=0; j<total;j++) {
  2304.             char *ch; char ic;
  2305.             ch = (char *)(ce->where.address.pixptr);
  2306.             ic = *(ch+j);
  2307.             fprintf(stderr,"%X.",ic);
  2308.                 }
  2309.         fprintf(stderr,"<- Now Emitting\n");
  2310.         }
  2311. ***************************************************************/
  2312. #ifdef USEPXL
  2313. #ifdef IBM3812
  2314.     if ((short)(ce->height) - ce->yOffset > 55) {
  2315.         ce->yyOffset = (short) ce->height - ce->yOffset;
  2316.         ce->yOffset  = (short) ce->height;
  2317.     } else {
  2318.         ce->yyOffset = (short) 0;
  2319.     }
  2320. #endif
  2321. #ifdef LJ
  2322.         ce->yyOffset = (short) 0;
  2323. #endif
  2324. #endif
  2325.  
  2326.     /* ce->cw = (long)(((double)ce->tfmw / (double)hconv) +0.5); */
  2327.     /* set active font to nn, load font pattern  xx ... */
  2328. #ifdef IBM3812
  2329.     PMPcont(total + 9);
  2330. #ifdef USEPXL
  2331.     sprintf(PMPformat, "\323%c\360%c%c%c",
  2332.         (unsigned char)fontptr->plusid,
  2333.         (unsigned char)VisChar((char)c),
  2334.         (unsigned char)ce->height,
  2335.         (unsigned char)ce->width);
  2336.     PMPout(6, PMPformat);
  2337.     PMPoutC((char)(-(ce->xOffset)));
  2338.     PMPoutC((char)(ce->cw - (-ce->xOffset + ce->width)));
  2339.     PMPoutC((char)(ce->yOffset));
  2340. #else
  2341.     sprintf(PMPformat, "\323%c\360%c%c%c",
  2342.         (unsigned char)fontptr->plusid,
  2343.         (unsigned char)VisChar((char)c),
  2344.         (unsigned char)num_rows,
  2345.         (unsigned char)num_cols);
  2346.     PMPout(6, PMPformat);
  2347.     PMPoutC((char)(-x_offset));
  2348.     PMPoutC((char)(ce->cw - (-x_offset + num_cols)));
  2349.     PMPoutC((char)num_rows-y_offset);
  2350. #endif
  2351. #endif
  2352. #ifdef LJ
  2353.     INT_ASCII(cnv_buffer,fontptr->plusid);
  2354.     EMIT(outfp, "\033*c%sd%dE\033(s%ldW", cnv_buffer,
  2355.         (unsigned int)VisChar((char)c), (long)(total + 16));
  2356.     EMITB(4, "\4\0\016\1");
  2357. /*    EMITC((char)(Landscape==_TRUE)); */
  2358.     EMITC((char)0);
  2359.     EMITC((char)0);
  2360. #ifdef USEPXL
  2361.     EMITWORD(-ce->xOffset);
  2362.     EMITWORD(ce->yOffset);
  2363.     EMITWORD(ce->width);
  2364.     EMITWORD(ce->height);
  2365. #else
  2366.     EMITWORD(-x_offset);
  2367.     EMITWORD(num_rows-y_offset);
  2368.     EMITWORD(num_cols);
  2369.     EMITWORD(num_rows);
  2370. #endif
  2371.     EMITWORD((int)ce->cw * 4);
  2372. #endif
  2373. #ifdef USEPXL
  2374.  
  2375.     if (fontptr->id == pk89)
  2376.         PkRaster(ce, _FALSE);
  2377.     else if (fontptr->id == id1002)
  2378.         for (i = 0; i < (int) ce->height; i++) {
  2379.             sl = ((unsigned char *)(ce->where.address.pixptr) + i * nbpl);
  2380.             EMITL(nbpl, sl);
  2381.         }
  2382.     else if (fontptr->id == id1001)
  2383.         for (i = 0; i < (int) ce->height; i++) {
  2384.             sl = (unsigned char *)(ce->where.address.pixptr + i * nwpl);
  2385.             EMITL(nbpl, sl);
  2386.         }
  2387. #else
  2388.     for (i = num_rows; i > 0; i--)
  2389.       {
  2390.         EMITL (nbpl, bits + (i-1) * nbpl);
  2391.       }
  2392. #endif
  2393. #ifdef IBM3812
  2394. #ifdef USEPXL
  2395.     used_fontstorage += (long)ce->height * ((ce->width + 15) >> 4)
  2396.         *2 + 14;
  2397. #else
  2398.     used_fontstorage += (long)num_rows * ((num_cols + 15) >> 4)
  2399.         *2 + 14;
  2400. #endif
  2401. #endif
  2402. #ifdef LJ
  2403. #ifdef USEPXL
  2404.     used_fontstorage += 64 * (((int)((ce->height * ce->width) - 1) / 64) + 1);
  2405. #else
  2406.     used_fontstorage += 64 * ((((num_rows * num_cols) - 1) / 64) + 1);
  2407. #endif
  2408. #endif
  2409.     fontptr->ncdl += 1;
  2410.     G_ncdl += 1;
  2411. }
  2412.  
  2413.  
  2414. /*-->RasterLine*/
  2415. /**********************************************************************/
  2416. /****************************  RasterLine  ****************************/
  2417. /**********************************************************************/
  2418. void
  2419. RasterLine(ce, nbpl, current_line, buffer)
  2420. struct char_entry *ce;
  2421. unsigned short  nbpl, current_line;
  2422. char    *buffer;
  2423. {
  2424. #ifdef IBM3812
  2425.     long    total;
  2426.     static unsigned short   maxlines;
  2427.  
  2428.     if (current_line == 0) {
  2429. #ifdef USEPXL
  2430.         maxlines = ce->height;
  2431.  
  2432.         MoveVert(-ce->yOffset);      /* move cursor up */
  2433.         MoveHor(-ce->xOffset);       /* move cursor right */
  2434. #else
  2435.         maxlines = num_rows;
  2436.  
  2437.         MoveVert(-(num_rows-y_offset));      /* move cursor up */
  2438.         MoveHor(-x_offset);       /* move cursor right */
  2439. #endif
  2440.         last_ry = UNKNOWN;       /* next time full positioning */
  2441.     }
  2442.  
  2443.     if (current_line % maxlines == 0) {
  2444.         if (current_line > 0) {    /* maxlines lines have been printed*/
  2445.             MoveVert(maxlines);   /*   move cursor down     */
  2446.             last_ry = UNKNOWN;    /* next time full positioning */
  2447.         }
  2448. #ifdef USEPXL
  2449.         total = (long)(ce->height - current_line) * (long)nbpl;
  2450. #else
  2451.         total = (long)(num_rows - current_line) * (long)nbpl;
  2452. #endif
  2453.         if ((total + 9) > 65535) {
  2454.             maxlines = (unsigned short)((65535 - 9) / nbpl);
  2455.             total = (long)maxlines * (long)nbpl;
  2456.         }
  2457.  
  2458.         PMPcont(total + 9);
  2459.         PMPout(2, "\365\0");
  2460.         EMITWORD(maxlines);
  2461. #ifdef USEPXL
  2462.         EMITWORD(ce->width);
  2463. #else
  2464.         EMITWORD(num_cols);
  2465. #endif
  2466.         PMPoutC((unsigned char) (total >> 16) & 0xFF);
  2467.         PMPoutC((unsigned char) (total >> 8 ) & 0xFF);
  2468.         PMPoutC((unsigned char)  total     & 0xFF);
  2469.     }
  2470.     EMITL((int)nbpl, buffer);
  2471. #endif
  2472. #ifdef LJ
  2473.     register int    emitbytes;
  2474.  
  2475.     for (emitbytes = (int)nbpl;
  2476.         (*(buffer + emitbytes - 1) == '\0') && (emitbytes > 0);
  2477.         emitbytes--) ;
  2478.     EMIT(outfp, "\033*b%dW", emitbytes);
  2479.     EMITL(emitbytes, buffer);
  2480. #endif
  2481. }
  2482.  
  2483.  
  2484. /*-->RasterChar*/
  2485. /**********************************************************************/
  2486. /****************************  RasterChar  ****************************/
  2487. /**********************************************************************/
  2488. void                     /* raster a character bitmap */
  2489. RasterChar(ce)
  2490. struct char_entry *ce;
  2491. {
  2492.     int     i;
  2493.     register unsigned char  *sl;
  2494.     unsigned short  nbpl, nwpl;
  2495.     unsigned char   raster_line_buf[BYTES_PER_PIXEL_LINE];
  2496.  
  2497. #ifdef DEBUG
  2498.     if (Debug)
  2499.         fprintf(stderr,"Raster character ...size=%d \n", (int)ce->charsize );
  2500. #endif
  2501.  
  2502. #ifdef USEPXL
  2503.     if (fontptr->id == pk89)   {
  2504.         nwpl = 0; /* initialize variable */
  2505.         nbpl = ((int)(ce->width) +  7) >> 3;
  2506.     } else if (fontptr->id == id1002)   {
  2507.         nwpl = 0; /* initialize variable */
  2508.         nbpl = ((int) ce->width +  7) >> 3;
  2509.     } else if (fontptr->id == id1001) {
  2510.         nwpl = ((int) ce->width + 31) >> 5;
  2511.         nbpl = ((int) ce->width + 7) >> 3;
  2512.     } else {
  2513.       /* should never be necessary */
  2514.       nwpl = 0;
  2515.       nbpl = 0;
  2516.     }
  2517. #else
  2518.     nbpl = (num_cols + 7) >> 3;
  2519. #endif
  2520.  
  2521. #ifdef LJ
  2522.     EMIT(outfp, "\033*t%dR\033*r1A",RESOLUTION);
  2523. #endif
  2524.                                             { /* read pixel from file */
  2525.     if ((ce->charsize == HUGE_SIZE) && (fontptr->id != pk89))
  2526.         OpenFontFile();
  2527. #ifdef USEPXL
  2528.         fseek(pxlfp, ce->where.address.fileOffset, 0);
  2529. #else
  2530.         fseek(gfin, ce->where.address.fileOffset, 0);
  2531.         gettochar();
  2532.         readbits();
  2533. #endif
  2534.     }
  2535.  
  2536. #ifdef USEPXL
  2537.     if (fontptr->id == pk89)
  2538.         PkRaster(ce, _TRUE);
  2539.     else if (fontptr->id == id1002) {
  2540.         for (i = 0; i < (int) ce->height; i++) {
  2541.             if (ce->charsize == HUGE_SIZE) {
  2542.                 fread(raster_line_buf, 1, (int) nbpl, pxlfp);
  2543.                 sl = raster_line_buf;
  2544.             } else
  2545.                 sl = ((unsigned char *)(ce->where.address.pixptr)
  2546.                     + i * nbpl);
  2547.  
  2548.             RasterLine(ce, (unsigned int)nbpl, i, sl);
  2549.         }
  2550.     } else if (fontptr->id == id1001) {
  2551.         long    filediff;
  2552.         filediff = (long)nwpl * 4 - nbpl;
  2553.         for (i = 0; i < (int) ce->height; i++) {
  2554.             if (ce->charsize == HUGE_SIZE) {
  2555.                 fread(raster_line_buf, 1, (int) nbpl, pxlfp);
  2556.                 /* skip fill bytes */
  2557.                 fseek(pxlfp, filediff, 1);
  2558.                 sl = raster_line_buf;
  2559.             } else
  2560.                 sl = (unsigned char *)(ce->where.address.pixptr + i * nwpl);
  2561.  
  2562.             RasterLine(ce, (unsigned int)nbpl, i, sl);
  2563.         }
  2564.     }
  2565. #else
  2566.     for (i = num_rows; i > 0; i--)
  2567.       RasterLine(ce, (unsigned int)nbpl, i, bits + (i-1) * nbpl);
  2568. #endif
  2569. #ifdef LJ
  2570.     EMIT(outfp, "\033*rB");
  2571. #endif
  2572.     last_ry = UNKNOWN;
  2573. }
  2574.  
  2575.  
  2576. /*-->Fatal*/
  2577. /**********************************************************************/
  2578. /******************************  Fatal  *******************************/
  2579. /**********************************************************************/
  2580. void
  2581. Fatal(fmt, a, b, c)      /* issue a fatal error message */
  2582. char    *fmt;           /* format              */
  2583. char    *a, *b, *c;     /* arguments           */
  2584. {
  2585.     fprintf(stderr, "\n");
  2586.     fprintf(stderr, "%s: FATAL--", G_progname);
  2587.     fprintf(stderr, fmt, a, b, c);
  2588.     fprintf(stderr, "\n\n");
  2589. #ifndef vms
  2590.     exit(2);
  2591. #else
  2592.     exit(SS$_ABORT);
  2593. #endif
  2594. }
  2595.  
  2596.  
  2597. /*-->FindPostAmblePtr*/
  2598. /**********************************************************************/
  2599. /************************  FindPostAmblePtr  **************************/
  2600. /**********************************************************************/
  2601. void
  2602. FindPostAmblePtr(postambleptr)
  2603. long    *postambleptr;
  2604. /* this routine will move to the end of the file and find the start
  2605.     of the postamble */
  2606. {
  2607.     long    i;
  2608.     fseek (dvifp,  0l, 2);   /* goto end of file */
  2609.     *postambleptr = ftell (dvifp) - 4;
  2610.     fseek (dvifp, *postambleptr, 0);
  2611.     while (_TRUE) {
  2612.         fseek (dvifp, --(*postambleptr), 0);
  2613.         if (((i = NoSignExtend(dvifp, 1)) != 223) &&
  2614.             (i != DVIFORMAT))
  2615.             Fatal ("Bad end of DVI file");
  2616.         if (i == DVIFORMAT)
  2617.             break;
  2618.     }
  2619.     fseek (dvifp, (*postambleptr) - 4, 0);
  2620.     (*postambleptr) = NoSignExtend(dvifp, 4);
  2621.     fseek (dvifp, *postambleptr, 0);
  2622. }
  2623.  
  2624.  
  2625. /*-->GetBytes*/
  2626. /**********************************************************************/
  2627. /*****************************  GetBytes  *****************************/
  2628. /**********************************************************************/
  2629. void
  2630. GetBytes(fp, cp, n)     /* get n bytes from file fp */
  2631. /*register*/ FILE *fp;      /* file pointer  */
  2632. register char   *cp;    /* character pointer */
  2633. int     n;            /* number of bytes  */
  2634. {
  2635.     while (n--)
  2636.         *cp++ = (char)getc(fp);
  2637. }
  2638.  
  2639.  
  2640. /*-->GetFontDef*/
  2641. /**********************************************************************/
  2642. /**************************** GetFontDef  *****************************/
  2643. /**********************************************************************/
  2644. void
  2645. GetFontDef()
  2646. /***********************************************************************
  2647.    Read the font  definitions as they  are in the  postamble of the  DVI
  2648.    file.
  2649. ***********************************************************************/
  2650. {
  2651.     unsigned char   byte;
  2652.     while (((byte = (unsigned char) NoSignExtend(dvifp, 1)) >= FNT_DEF1) &&
  2653.         (byte <= FNT_DEF4)) {
  2654.         switch (byte) {
  2655.         case FNT_DEF1:
  2656.             ReadFontDef ( NoSignExtend(dvifp, 1));
  2657.             break;
  2658.         case FNT_DEF2:
  2659.             ReadFontDef ( NoSignExtend(dvifp, 2));
  2660.             break;
  2661.         case FNT_DEF3:
  2662.             ReadFontDef ( NoSignExtend(dvifp, 3));
  2663.             break;
  2664.         case FNT_DEF4:
  2665.             ReadFontDef ( NoSignExtend(dvifp, 4));
  2666.             break;
  2667.         default:
  2668.             Fatal ("Bad byte value in font defs");
  2669.             break;
  2670.         }
  2671.     }
  2672.     if (byte != POST_POST)
  2673.         Fatal ("POST_POST missing after fontdefs");
  2674. }
  2675.  
  2676.  
  2677. /*-->GetKeyStr*/
  2678. /**********************************************************************/
  2679. /*****************************  GetKeyStr  ****************************/
  2680. /**********************************************************************/
  2681. /* extract first keyword-value pair from string (value part may be null)
  2682.  * return pointer to remainder of string
  2683.  * return NULL if none found
  2684.  */
  2685. char    KeyStr[STRSIZE];
  2686. char    ValStr[STRSIZE];
  2687. char
  2688. *GetKeyStr( str, kw )
  2689. char    *str;
  2690. KeyWord *kw;
  2691. {
  2692.     char    *s, *k, *v, t;
  2693.     if ( !str )
  2694.         return( NULL );
  2695.     for ( s = str; *s == ' '; s++)
  2696.         ;          /* skip over blanks */
  2697.     if ( *s == '\0' )
  2698.         return( NULL );
  2699.     for ( k = KeyStr; /* extract keyword portion */
  2700.     *s != ' ' && *s != '\0' && *s != '=';
  2701.         *k++ = *s++)
  2702.         ;
  2703.     *k = '\0';
  2704.     kw->Key = KeyStr;
  2705.     kw->Val = v = NULL;
  2706.     kw->vt = None;
  2707.     for ( ; *s == ' '; s++)
  2708.         ;            /* skip over blanks */
  2709.     if ( *s != '=' )         /* look for "=" */
  2710.         return( s );
  2711.     for ( s++; *s == ' '; s++);      /* skip over blanks */
  2712.     if ( *s == '\'' || *s == '\"' )  /* get string delimiter */
  2713.         t = *s++;
  2714.     else
  2715.         t = ' ';
  2716.     for ( v = ValStr; /* copy value portion up to delim */
  2717.     *s != t && *s != '\0';
  2718.         *v++ = *s++)
  2719.         ;
  2720.     if ( t != ' ' && *s == t )
  2721.         s++;
  2722.     *v = '\0';
  2723.     kw->Val = ValStr;
  2724.     kw->vt = String;
  2725.     return( s );
  2726. }
  2727.  
  2728.  
  2729. /*-->GetKeyVal*/
  2730. /**********************************************************************/
  2731. /*****************************  GetKeyVal  ****************************/
  2732. /**********************************************************************/
  2733. /* get next keyword-value pair decode value according to table entry  */
  2734. bool
  2735. GetKeyVal( kw, tab, nt, tno)
  2736. KeyWord *kw;
  2737. KeyDesc tab[];
  2738. int     nt;
  2739. int     *tno;
  2740. {
  2741.     int     i;
  2742.     char    c = '\0';
  2743.     *tno = -1;
  2744.     for (i = 0; i < nt; i++)
  2745.         if ( IsSame(kw->Key, tab[i].Entry) ) {
  2746.             *tno = i;
  2747.             switch ( tab[i].Typ ) {
  2748.             case None:
  2749.                 if ( kw->vt != None )
  2750.                     return( _FALSE );
  2751.                 break;
  2752.             case String:
  2753.                 if ( kw->vt != String )
  2754.                     return( _FALSE );
  2755.                 break;
  2756.             case Integer:
  2757.                 if ( kw->vt != String )
  2758.                     return( _FALSE );
  2759.                 if ( sscanf(kw->Val, "%d%c",
  2760.                     &(kw->v.i), &c) != 1
  2761.                      || c != '\0' )
  2762.                     return( _FALSE );
  2763.                 break;
  2764. /*              case Number:
  2765.  *              case Dimension:
  2766.  *                  if( kw->vt != String ) return( _FALSE );
  2767.  *                  if( sscanf(kw->Val,"%f%c",
  2768.  *                     &(kw->v.n), &c) != 1
  2769.  *                  || c != '\0' ) return( _FALSE );
  2770.  *                  break;
  2771.  */
  2772.             }
  2773.             kw->vt = tab[i].Typ;
  2774.             return( _TRUE );
  2775.         }
  2776.     return( _TRUE );
  2777. }
  2778.  
  2779.  
  2780.  
  2781. /*-->IsSame*/
  2782. /**********************************************************************/
  2783. /*******************************  IsSame  *****************************/
  2784. /**********************************************************************/
  2785. bool                  /* compare strings, ignore case */
  2786. IsSame(a, b)
  2787. char    *a, *b;
  2788. {
  2789.     char *x, *y;
  2790.     x = a;
  2791.     y = b;
  2792.     for ( ; *a != '\0'; a++,b++)
  2793.         if ( tolower(*a) != tolower(*b) )
  2794.             return( _FALSE );
  2795.     return( *x == *y ? _TRUE : _FALSE );
  2796. }
  2797.  
  2798.  
  2799. /*-->NoSignExtend*/
  2800. /**********************************************************************/
  2801. /***************************  NoSignExtend  ***************************/
  2802. /**********************************************************************/
  2803. long
  2804. NoSignExtend(fp, n)     /* return n byte quantity from file fd */
  2805. register FILE *fp;      /* file pointer    */
  2806. register int    n;      /* number of bytes */
  2807. {
  2808.     long    x;      /* number being constructed */
  2809.     x = 0;
  2810.     while (n--)  {
  2811.         x <<= 8;
  2812.         x |= getc(fp);
  2813.     }
  2814.     return(x);
  2815. }
  2816.  
  2817.  
  2818. /*-->OpenFontFile*/
  2819. /**********************************************************************/
  2820. /************************** OpenFontFile  *****************************/
  2821. /**********************************************************************/
  2822. void
  2823. OpenFontFile()
  2824. /***********************************************************************
  2825.     The original version of this dvi driver reopened the font file  each
  2826.     time the font changed, resulting in an enormous number of relatively
  2827.     expensive file  openings.   This version  keeps  a cache  of  up  to
  2828.     MAXOPEN open files,  so that when  a font change  is made, the  file
  2829.     pointer, pxlfp, can  usually be  updated from the  cache.  When  the
  2830.     file is not found in  the cache, it must  be opened.  In this  case,
  2831.     the next empty slot  in the cache  is assigned, or  if the cache  is
  2832.     full, the least used font file is closed and its slot reassigned for
  2833.     the new file.  Identification of the least used file is based on the
  2834.     counts of the number  of times each file  has been "opened" by  this
  2835.     routine.  On return, the file pointer is always repositioned to  the
  2836.     beginning of the file.
  2837. ***********************************************************************/
  2838. {
  2839.     int     i, least_used, current;
  2840.     struct pixel_list tmp;
  2841.  
  2842. #ifdef DEBUG
  2843.     if (Debug)
  2844.         fprintf(stderr,"open font file %p\n", fontptr->font_file_id);
  2845. #endif
  2846. /*
  2847. fprintf(stderr,"? %lx == %lx\n", pfontptr,fontptr);
  2848. */
  2849.     if ((pfontptr == fontptr) && (pxlfp != NO_FILE))
  2850.         return;         /* we need not have been called */
  2851.  
  2852.        if (fontptr->font_file_id == NO_FILE)
  2853.         return;         /* we need not have been called */
  2854.  
  2855.     tmp = pixel_files[1];
  2856.     for (current = 1;
  2857.         (current <= nopen) && (tmp.pixel_file_id != fontptr->font_file_id); ) {
  2858.         ++current;
  2859.         tmp = pixel_files[current];
  2860.     }
  2861.     /* try to find file in open list */
  2862.  
  2863.     if (current <= nopen)       /* file already open */ {
  2864.         if ( (pxlfp = pixel_files[current].pixel_file_id) != NO_FILE )
  2865.             fseek(pxlfp, 0l, 0);
  2866.             /* reposition to start of file */
  2867.     } else {
  2868.             /* file not in open list          */
  2869.         if (nopen < MAXOPEN)    /* just add it to list    */
  2870.             current = ++nopen;
  2871.         else  {
  2872.             /* list full -- find least used file,     */
  2873.             /* close it, and reuse slot for new file  */
  2874.             least_used = 1;
  2875.             for (i = 2; i <= MAXOPEN; ++i)
  2876.                 if (pixel_files[least_used].use_count>pixel_files[i].use_count)
  2877.                     least_used = i;
  2878.             if (pixel_files[least_used].pixel_file_id != NO_FILE) {
  2879.                 FILE * fid;
  2880.                 struct font_entry *fp;
  2881.                 fid = pixel_files[least_used].pixel_file_id;
  2882.                 /* mark file as being closed in the entry */
  2883.                 fp = hfontptr;
  2884.                 while (fp != NULL && fp->font_file_id != fid) fp = fp->next;
  2885.                 if (fp == NULL)
  2886.                    Fatal("Open file %x not found in font entry list.\n", fid);
  2887.                 else {
  2888.                     fp->font_file_id = NULL;
  2889.                 }
  2890.                 fclose( fid );
  2891.             }
  2892. #ifdef DEBUG
  2893.             if (Debug)
  2894.                  fprintf(stderr,"\n__reuse slot %d\n", least_used);
  2895. #endif
  2896.             current = least_used;
  2897.         }
  2898.         if ((pxlfp = BINOPEN(fontptr->name)) == NULL) {
  2899.             Warning("PXL-file %s could not be opened", fontptr->name);
  2900.             pxlfp = NO_FILE;
  2901.         } else {
  2902. #ifdef DEBUG
  2903.              if (Debug) 
  2904.            fprintf(stderr,"Opening File  <%s> /%p/, Size(font_entry)=%d\n",
  2905.                fontptr->name, pxlfp, sizeof(struct font_entry ));
  2906. #endif
  2907.  
  2908.         }
  2909.         pixel_files[current].pixel_file_id = pxlfp;
  2910.         pixel_files[current].use_count = 0;
  2911.     }
  2912.     pfontptr = fontptr;         /* make previous = current font */
  2913.     fontptr->font_file_id = pxlfp;      /* set file identifier */
  2914.     pixel_files[current].use_count++;   /* update reference count */
  2915. #ifndef USEPXL
  2916.     gfin = pxlfp;
  2917. #endif
  2918. }
  2919.  
  2920.  
  2921. /*-->PixRound*/
  2922. /**********************************************************************/
  2923. /*****************************  PixRound  *****************************/
  2924. /**********************************************************************/
  2925. long
  2926. PixRound(x, conv)       /* return rounded number of pixels */
  2927. long    x;          /* in DVI units     */
  2928. long    conv;       /* conversion factor */
  2929. {
  2930.     return((x + conv) / conv);
  2931. }
  2932.  
  2933. #ifdef LJ_RESIDENT_FONTS
  2934. /*-->TryResident*/
  2935. /**********************************************************************/
  2936. /****************************  TryResident  ***************************/
  2937. /**********************************************************************/
  2938. bool
  2939. TryResident(fontptr)
  2940. struct font_entry *fontptr;
  2941. {
  2942.   extern bool tfm_read_info ();
  2943.   tfm_info_type tfm_info;
  2944.   
  2945.   /* To determine if a font is resident, check for a special family
  2946.      value (header bytes 12..16 in the TFM file). This seems cleaner,
  2947.      and certainly more convenient, than somehow reading an external
  2948.      ljfonts.map file in which we'd have to specify information for all
  2949.      the resident fonts.  */
  2950.   if (tfm_read_info (fontptr->n, &tfm_info)
  2951.       && tfm_info.family[0]
  2952.       && strcmp (tfm_info.family, "HPAUTOTFM") == 0) {
  2953.     unsigned i;
  2954.     double factor = fontptr->s / (double) 0x100000;
  2955.  
  2956.     resident_count++;
  2957.     fontptr->resident_p = _TRUE;
  2958.     strcpy (fontptr->symbol_set, tfm_info.coding_scheme);
  2959.     fontptr->resid = tfm_info.typeface_id;
  2960.     fontptr->spacing = tfm_info.spacing;
  2961.     fontptr->style = tfm_info.style;
  2962.     fontptr->weight = tfm_info.weight;
  2963.  
  2964.     if (fontptr->spacing == SPACING_FIXED) {
  2965.       /* Have to select the point in pitch (characters per inch) instead
  2966.          of point size, and thus have to figure out the pitch that
  2967.          corresponds to the point size at which the font is used.
  2968.          
  2969.          To do this, take the width of the interword space, and see how
  2970.          many of those characters will fit in the at size. Then convert
  2971.          to how many characters will fit in one inch. That's our pitch.
  2972.          
  2973.          All the builtin LJ4 fonts that are monospaced are Intellifont,
  2974.          which have 72.307 points per inch. Not that it really makes any
  2975.          difference. We don't worry about this elsewhere, since all
  2976.          point sizes are rounded to .25pt anyway, which is more than the
  2977.          difference between the various definitions of `point'. */
  2978.       double ds_in_points = fontptr->s / 65536.0;
  2979.       double w_in_points = tfm_info.interword / (double) 0x100000;
  2980.       if (ds_in_points == 0 || w_in_points == 0) {
  2981.         /* Avoid division by zero if no interword space. */
  2982.         Warning ("%s: Can't determine pitch for this monospaced font.\n", 
  2983.                  fontptr->n);
  2984.         fontptr->pitch = 10; /* Result will look awful, which is good. */
  2985.       } else {
  2986.         fontptr->pitch = 72.307 / (ds_in_points * w_in_points);
  2987.       }
  2988.     }
  2989.     
  2990. #ifdef DEBUG
  2991.     if (Debug)
  2992.       fprintf(stderr, "%6s: typeface=%u\tspacing=%u\tstyle=%u\tweight=%d\n",
  2993.               fontptr->n, fontptr->resid, fontptr->spacing,
  2994.               fontptr->style, fontptr->weight);
  2995. #endif
  2996.     for (i = 0; i < NFNTCHARS; i++) {
  2997.       struct char_entry *cptr = &(fontptr->ch[i]);
  2998.       cptr->tfmw = (long) (tfm_info.widths[i] * factor);
  2999.       cptr->cw = ((fontptr->ch[i].tfmw) / (double) hconv) + .5;
  3000.       cptr->width = 
  3001.     cptr->height = 
  3002.       cptr->xOffset = 
  3003.         cptr->yOffset = 
  3004.           cptr->yyOffset = 0;
  3005.     }
  3006.     return _TRUE;
  3007.   } else {
  3008.     fontptr->resident_p = _FALSE;
  3009.  
  3010.     if (tfm_info.family[0]
  3011.     && strcmp (tfm_info.family, "UNSPECIFIED") == 0) {
  3012.       Warning("font family for %s is UNSPECIFIED; need to run dvicopy?",
  3013.           fontptr->n);
  3014.       fontptr->font_file_id = NO_FILE;
  3015.       return _TRUE;
  3016.     } else {
  3017.       return _FALSE;
  3018.     }
  3019.   }
  3020. }
  3021. #endif
  3022.  
  3023.  
  3024.  
  3025. /*-->ReadFontDef*/
  3026. /**********************************************************************/
  3027. /****************************  ReadFontDef  ***************************/
  3028. /**********************************************************************/
  3029. void
  3030. ReadFontDef(k)
  3031. long    k;
  3032. {
  3033.     long    t;
  3034.     unsigned short i;
  3035.     struct font_entry *tfontptr; /* temporary font_entry pointer   */
  3036.     struct char_entry *tcharptr; /* temporary char_entry pointer  */
  3037.     static int      plusid = 0;
  3038.     bool font_found = _FALSE;
  3039. #ifdef LJ_RESIDENT_FONTS
  3040.     bool resident_font_located = _FALSE;
  3041. #endif
  3042. #ifdef LJ
  3043.     int depth, max_depth;
  3044. #endif
  3045.  
  3046. #ifdef DEBUG
  3047.     if (Debug)
  3048.       fprintf(stderr,"Mallocating %d Bytes)...\n", sizeof(struct font_entry ));
  3049. #endif
  3050.  
  3051.     if ((tfontptr = NEW(struct font_entry )) == NULL)
  3052.         Fatal("can't malloc space for font_entry");
  3053.  
  3054.     allocated_storage += sizeof(struct font_entry );
  3055.  
  3056.     tfontptr->next = hfontptr;
  3057.     tfontptr->font_file_id = NULL;
  3058.     fontptr = hfontptr = tfontptr;
  3059.     tfontptr->ncdl = 0;
  3060.     tfontptr->k = k;
  3061.     tfontptr->c = NoSignExtend(dvifp, 4); /* checksum */
  3062.     tfontptr->s = NoSignExtend(dvifp, 4); /* space size */
  3063.     tfontptr->d = NoSignExtend(dvifp, 4); /* design size */
  3064.     tfontptr->a = (int) NoSignExtend(dvifp, 1); /* length for font name */
  3065.     tfontptr->l = (int) NoSignExtend(dvifp, 1); /* device length */
  3066.  
  3067. #ifdef LJ
  3068.     tfontptr->max_width = tfontptr->max_height = tfontptr->max_yoff =
  3069.                           max_depth = 0;
  3070. #endif
  3071.  
  3072.     GetBytes(dvifp, tfontptr->n, tfontptr->a + tfontptr->l);
  3073.     tfontptr->n[tfontptr->a+tfontptr->l] = '\0';
  3074.  
  3075.     tfontptr->font_mag = (long)((
  3076.          ActualFactor((long)(1000.0*tfontptr->s/(double)tfontptr->d+0.5))
  3077.          * ActualFactor(mag)
  3078. #ifdef USEPXL
  3079.          * RESOLUTION * 5.0
  3080. #else
  3081.          * RESOLUTION
  3082. #endif
  3083.            ) + 0.5);
  3084. /*
  3085. printf("[%ld]=%lf * %lf * %lf + 0.5 = %ld\n",
  3086.     ((long)(1000.0*tfontptr->s/(double)tfontptr->d+0.5)),
  3087.     ActualFactor((long)(1000.0*tfontptr->s/(double)tfontptr->d+0.5)),
  3088.     ActualFactor(mag),
  3089.     (double)RESOLUTION * 5,
  3090.     tfontptr->font_mag );
  3091. */
  3092.  
  3093. #ifdef LJ_RESIDENT_FONTS
  3094.     /* Pass in the name; fills in resident_p and resid (if resident). */
  3095.  
  3096.     resident_font_located = (bool) TryResident(tfontptr);
  3097.     
  3098.     if (tfontptr->resident_p)
  3099.       return;
  3100.  
  3101.     if (!(resident_font_located)) {
  3102. #endif
  3103.  
  3104. #ifdef KPATHSEA
  3105.       {
  3106.        kpse_font_file_type font_ret;
  3107.        unsigned dpi = tfontptr->font_mag / 5.0 + .5;
  3108.        char *name = kpse_find_pk (tfontptr->n, dpi, &font_ret);
  3109.        if (name)
  3110.          {
  3111.            font_found = _TRUE;
  3112.            strcpy (tfontptr->name, name);
  3113.            free (name);
  3114.  
  3115.            if (!STREQ (tfontptr->n, font_ret.name))
  3116.              fprintf (stderr,
  3117.                       "dvilj: Font %s not found, using %s at %d instead.\n",
  3118.                       tfontptr->n, font_ret.name, font_ret.dpi);
  3119.            else if (!kpse_check_bitmap_tolerance ((double) font_ret.dpi,
  3120.                                                   (double) dpi))
  3121.              fprintf (stderr,
  3122.                       "dvilj: Font %s at %d not found, using %d instead.\n",
  3123.                       tfontptr->name, dpi, font_ret.dpi);
  3124.            if (!( (G_noverbatim) || (G_quiet) ) )
  3125.              fprintf(stderr,"%d: using font <%s>\n", plusid,tfontptr->name);
  3126.          }
  3127.        else
  3128.          {
  3129.            tfontptr->font_file_id = NO_FILE;
  3130.            fprintf (stderr,
  3131.              "dvilj: Font %s at %u not found, characters will be left blank.\n",
  3132.              tfontptr->n, dpi);
  3133.          }
  3134.      }
  3135. #else
  3136.  /* non-kpathsea font lookup */
  3137.       if (!(findfile(PXLpath, 
  3138.              tfontptr->n, 
  3139.              tfontptr->font_mag, 
  3140.              tfontptr->name,
  3141.              _FALSE,
  3142.              0))) {
  3143.     Warning(tfontptr->name); /* contains error messsage */
  3144.     tfontptr->font_file_id = NO_FILE;
  3145.       }
  3146.       else {
  3147.     font_found = _TRUE;
  3148.     if (!( (G_noverbatim) || (G_quiet) ) )
  3149.       fprintf(stderr,"%d: using font <%s>\n",
  3150.           plusid,tfontptr->name);
  3151.       }
  3152. #endif /* KPATHSEA */
  3153.  
  3154. #ifdef LJ_RESIDENT_FONTS
  3155.     }
  3156. #endif
  3157.  
  3158.     tfontptr->plusid = plusid;
  3159.     plusid++;
  3160.  
  3161.     /* sprintf(tfontptr->psname,"%s.%ld.%d",
  3162.        tfontptr->n,tfontptr->font_mag,tfontptr->plusid);*/
  3163.  
  3164. #ifdef LJ
  3165.     if (plusid >= HANDLE_MAX_FONTS)
  3166.         Fatal("can handle only %d fonts! ask a wizzard...\n",
  3167.                HANDLE_MAX_FONTS);
  3168. #endif
  3169.     if (tfontptr != pfontptr) {
  3170.         if (font_found) OpenFontFile();
  3171.         else
  3172.             pxlfp = NO_FILE;
  3173.     }
  3174. #ifdef USEPXL
  3175.     if ( pxlfp == NO_FILE ) {        /* allow missing pxl files */
  3176.         tfontptr->magnification = 0;
  3177.         tfontptr->designsize = 0;
  3178. #endif
  3179.         for (i = FIRSTFNTCHAR; i <= LASTFNTCHAR; i++) {
  3180.             tcharptr = &(tfontptr->ch[i]);
  3181. #ifdef USEPXL
  3182.             tcharptr->width = 0;
  3183.             tcharptr->height = 0;
  3184.             tcharptr->xOffset = 0;
  3185.             tcharptr->yOffset = 0;
  3186. #endif
  3187.             tcharptr->where.isloaded = _FALSE;
  3188.             tcharptr->where.address.fileOffset = NONEXISTANT;
  3189.             tcharptr->tfmw = 0;
  3190.         }
  3191. #ifdef USEPXL
  3192.         return;
  3193.     }
  3194.     t = (long) NoSignExtend(pxlfp, 1);
  3195.     if (t == 0) {
  3196.         t = (long) NoSignExtend(pxlfp, 1);
  3197.         t = (long) NoSignExtend(pxlfp, 2);
  3198.         if (t == 1002)
  3199.             tfontptr->id = id1002;
  3200.         else if (t == 1001)
  3201.             tfontptr->id = id1001;
  3202.         else
  3203.             Fatal("Unknown Version of PXL-format\n");
  3204.     } else {
  3205.         if (t == PK_PRE)    {
  3206.             unsigned char   temp_byte;
  3207.             temp_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  3208.             if (temp_byte != PK_ID) Fatal(
  3209.                "Wrong Version of pk file!  (%d should be 89)\n",
  3210.                              (int)temp_byte);
  3211.             else
  3212.                 tfontptr->id = pk89;
  3213.         } else
  3214.             Fatal("unknown font format in file <%s> !\n",fontptr->name);
  3215.     }
  3216.  
  3217.     if ((tfontptr->id == id1002) || (tfontptr->id == id1001)) {
  3218.         fseek(pxlfp, -20l, 2);
  3219.  
  3220.         t = NoSignExtend(pxlfp, 4);
  3221.         if ((tfontptr->c != 0) && (t != 0) && (tfontptr->c != t))
  3222.     Warning("font = \"%s\",\n->tfm checksum = %lX,\n->pxl checksum = %lX",
  3223.                           tfontptr->name, tfontptr->c, t);
  3224.         tfontptr->magnification = NoSignExtend(pxlfp, 4);
  3225.         tfontptr->designsize    = NoSignExtend(pxlfp, 4);
  3226.  
  3227.         if (tfontptr->id == id1001)
  3228.             fseek(pxlfp, (long) (NoSignExtend(pxlfp, 4) * 4), 0);
  3229.         else
  3230.             fseek(pxlfp, (long) NoSignExtend(pxlfp, 4) , 0);
  3231.  
  3232.         for (i = FIRSTFNTCHAR; i <= 127; i++) {   /* only defined for 7bit*/
  3233.             tcharptr = &(tfontptr->ch[i]);
  3234.             tcharptr->width   = (unsigned short) NoSignExtend(pxlfp, 2);
  3235.             tcharptr->height  = (unsigned short) NoSignExtend(pxlfp, 2);
  3236.             tcharptr->xOffset = (short) SignExtend(pxlfp, 2);
  3237.             tcharptr->yOffset = (short) SignExtend(pxlfp, 2);
  3238.             tcharptr->where.isloaded = _FALSE;
  3239.             if (tfontptr->id == id1001)
  3240.                 tcharptr->where.address.fileOffset = NoSignExtend(pxlfp,4) * 4;
  3241.             else
  3242.                 tcharptr->where.address.fileOffset = NoSignExtend(pxlfp,4);
  3243.             tcharptr->tfmw = (long)
  3244.             (   (double)(NoSignExtend(pxlfp, 4))
  3245.               * (double)tfontptr->s / (double) 0x100000 );
  3246.             tcharptr->cw = (long)(((double)tcharptr->tfmw/(double)hconv) + 0.5);
  3247.  
  3248.             if (tcharptr->width  > CHAR_WIDTH_LARGE  ||
  3249.                 tcharptr->height > CHAR_HEIGTH_LARGE )
  3250.                 tcharptr->charsize = LARGE_SIZE;
  3251.             else
  3252.                 tcharptr->charsize = SMALL_SIZE;
  3253. #ifdef LJ
  3254.             max(tfontptr->max_width,tcharptr->width);
  3255.             max(tfontptr->max_height,tcharptr->height);
  3256.             if (tcharptr->yOffset > 0  && (int)tfontptr->max_yoff < (int)tcharptr->yOffset) 
  3257.           tfontptr->max_yoff = tcharptr->yOffset;
  3258.             if ((depth = tcharptr->height - tcharptr->yOffset)>max_depth)
  3259.                    max_depth = depth;
  3260. #endif
  3261.  
  3262.         }
  3263. #ifdef LJ
  3264.         tfontptr->max_height = max_depth ? tfontptr->max_yoff+max_depth :
  3265.                                            tfontptr->max_yoff+1;
  3266. #endif
  3267.     } else { /* PK 89 format */
  3268.         unsigned char   temp_byte;
  3269.         register unsigned char  flag_byte;
  3270.         long    hppp, vppp, pkloc, packet_length;
  3271.         int     car, ii;
  3272.  
  3273.         /* read comment */
  3274.         for ( ii = temp_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  3275.               ii>0; ii--) {
  3276.             flag_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  3277. #ifdef DEBUG
  3278.             if (Debug) fprintf(stderr, "%c", flag_byte ) ;
  3279. #endif
  3280.         }
  3281. #ifdef DEBUG
  3282.         if (Debug) fprintf(stderr, "\n");
  3283. #endif
  3284.         pkloc = 3 + (int)temp_byte;
  3285.         tfontptr->designsize = NoSignExtend(pxlfp, 4);
  3286.  
  3287.         t = NoSignExtend(pxlfp, 4);
  3288.         if ((tfontptr->c != 0) && (t != 0) && (tfontptr->c != t))
  3289.           Warning("font = \"%s\",\n->tfm checksum = %lX,\n->pxl checksum = %lX",
  3290.                  tfontptr->name, tfontptr->c, t);
  3291.  
  3292.         hppp = NoSignExtend(pxlfp, 4);
  3293.         vppp = NoSignExtend(pxlfp, 4);
  3294.         if (hppp != vppp)
  3295.             Warning("aspect ratio is %ld:%ld (should be 1:1)!", hppp,vppp);
  3296.         tfontptr->magnification = (long)(hppp * 72.27 * 5 / 65536l + 0.5);
  3297.  
  3298.         pkloc += 16;
  3299.         flag_byte = skip_specials(&pkloc);
  3300.  
  3301.         while (flag_byte != PK_POST) {
  3302.         if ((flag_byte & 7) == 7) {
  3303.         /* fprintf(stderr,"\nRead long character preamble\n"); */
  3304.  
  3305.            packet_length = (unsigned long)NoSignExtend(pxlfp,4);
  3306.            if ((car = (int)NoSignExtend(pxlfp, 4)) > (LASTFNTCHAR))
  3307.                 Fatal("Bad character (%d) in PK-File\n",(int)car) ;
  3308.  
  3309.            tcharptr = &(tfontptr->ch[car]);
  3310.            tcharptr->where.address.fileOffset = pkloc;
  3311.            /* set pkloc to end_of_packet */
  3312.            pkloc += packet_length + 8;
  3313.  
  3314.            tcharptr->tfmw = (long) NoSignExtend(pxlfp, 4);
  3315.            (void) NoSignExtend(pxlfp, 4); /* horesc not used */
  3316.            (void) NoSignExtend(pxlfp, 4); /* not used */
  3317.  
  3318.            tcharptr ->width   = (unsigned short) NoSignExtend(pxlfp, 4);
  3319.            tcharptr ->height  = (unsigned short) NoSignExtend(pxlfp, 4);
  3320.            tcharptr ->xOffset = (short) SignExtend(pxlfp, 4);
  3321.            tcharptr ->yOffset = (short) SignExtend(pxlfp, 4);
  3322.            tcharptr ->where.isloaded = _FALSE;
  3323.         } else if (flag_byte & 4) {
  3324.             /* fprintf(stderr,"Read extended short character preamble\n"); */
  3325.  
  3326.             packet_length = ((long) flag_byte & 3) * 65536l +
  3327.                 (unsigned short) NoSignExtend(pxlfp, 2);
  3328.             if ((car = (int)NoSignExtend(pxlfp, 1)) > (LASTFNTCHAR))
  3329.                 Fatal("Bad character (%d) in PK-File\n",(int)car) ;
  3330.  
  3331.             tcharptr = &(tfontptr->ch[car]);
  3332.             tcharptr->where.address.fileOffset = pkloc;
  3333.             /* set pkloc to end_of_packet */
  3334.             pkloc += packet_length + 3;
  3335.  
  3336.             tcharptr->tfmw = (long) NoSignExtend(pxlfp, 3);
  3337. /*
  3338.             { register unsigned short t;
  3339.               t = (unsigned short) NoSignExtend(pxlfp, 1);
  3340.               tcharptr->tfmw = t * 65536l +
  3341.               (unsigned short) NoSignExtend(pxlfp, 2);
  3342.             }
  3343. */
  3344.             /* horesc not used */
  3345.             (void) NoSignExtend(pxlfp, 2) ;
  3346.             tcharptr ->width   = (unsigned short) NoSignExtend(pxlfp,2);
  3347.             tcharptr ->height  = (unsigned short) NoSignExtend(pxlfp,2);
  3348.             tcharptr ->xOffset = (short) SignExtend(pxlfp, 2);
  3349.             tcharptr ->yOffset = (short) SignExtend(pxlfp, 2);
  3350.             tcharptr ->where.isloaded = _FALSE;
  3351.         } else {
  3352.             /* fprintf(stderr,"<Read short character preamble@>\n"); */
  3353.  
  3354.             packet_length = ((long)flag_byte & 3) * 256 +
  3355.                 NoSignExtend(pxlfp, 1) ;
  3356.             if ((car = (int)NoSignExtend(pxlfp, 1)) > (LASTFNTCHAR))
  3357.                 Fatal("Bad character (%d) in PK-File\n",(int)car) ;
  3358.  
  3359.             tcharptr = &(tfontptr->ch[car]);
  3360.             tcharptr->where.address.fileOffset = pkloc;
  3361.             /* set pkloc to end_of_packet */
  3362.             pkloc += packet_length + 2 ;
  3363.  
  3364.             tcharptr->tfmw = (long) NoSignExtend(pxlfp, 3);
  3365. /*
  3366.             { register unsigned short t;
  3367.               t = (unsigned short) NoSignExtend(pxlfp, 1);
  3368.               tcharptr->tfmw = t * 65536l +
  3369.               (unsigned short) NoSignExtend(pxlfp, 2);
  3370.             }
  3371. */
  3372.             /* horesc not used */
  3373.             (void) NoSignExtend(pxlfp, 1) ;
  3374.             tcharptr ->width   = (unsigned short) NoSignExtend(pxlfp,1);
  3375.             tcharptr ->height  = (unsigned short) NoSignExtend(pxlfp,1);
  3376.             tcharptr ->xOffset = (short) SignExtend(pxlfp, 1);
  3377.             tcharptr ->yOffset = (short) SignExtend(pxlfp, 1);
  3378.             tcharptr ->where.isloaded = _FALSE;
  3379.         }
  3380.  
  3381.         tcharptr->tfmw = (long)
  3382.            ( tcharptr->tfmw * (double)tfontptr->s / (double) 0x100000 );
  3383.  
  3384.         tcharptr->cw = (long)(((double)tcharptr->tfmw /
  3385.             (double)hconv) + 0.5);
  3386.  
  3387.         if (tcharptr->width  > CHAR_WIDTH_LARGE  ||
  3388.             tcharptr->height > CHAR_HEIGTH_LARGE )
  3389.             tcharptr->charsize = LARGE_SIZE;
  3390.         else
  3391.             tcharptr->charsize = SMALL_SIZE;
  3392.  
  3393. #ifdef LJ
  3394. /*
  3395. printf("char=%d: this=%d, max_width=%d, this=%d,max_height=%d, this=%d,max_yoff=%d\n",
  3396.        car, tcharptr->width, tfontptr->max_width,
  3397.        tcharptr->height,tfontptr->max_height, 
  3398.        tcharptr->yOffset,tfontptr->max_yoff);
  3399. */
  3400.         max(tfontptr->max_width, tcharptr->width);
  3401.         max(tfontptr->max_height,tcharptr->height);
  3402.     if (tcharptr->yOffset > 0  && (int)tfontptr->max_yoff < (int)tcharptr->yOffset)
  3403.       tfontptr->max_yoff = tcharptr->yOffset;
  3404.  
  3405.         if ((depth = tcharptr->height - tcharptr->yOffset) > max_depth)
  3406.       max_depth = depth;
  3407. #endif
  3408. /*
  3409. fprintf(stderr,"char=%d, yotcharptr=%lx, flag_byte=%d, font=%lx\n",car, tcharptr,flag_byte,tfontptr);
  3410. */
  3411.         tcharptr->flag_byte = flag_byte;
  3412.         fseek(pxlfp, (long) pkloc, 0);
  3413.         flag_byte = skip_specials(&pkloc);
  3414.  
  3415.         } /* end of while */
  3416. #ifdef LJ
  3417. tfontptr->max_height = max_depth ? tfontptr->max_yoff+max_depth :
  3418.                                    tfontptr->max_yoff+1;
  3419. #endif
  3420.  
  3421. /*
  3422. printf("fontid=%d: max_width=%u, max_height=%d, max_yoff=%u\n",
  3423.         tfontptr->plusid, tfontptr->max_width,
  3424.         tfontptr->max_height, tfontptr->max_yoff);
  3425. */
  3426.  
  3427. #else
  3428.     if ( pxlfp == NO_FILE )        /* allow missing pxl files */
  3429.     return;
  3430.  
  3431.     gfin = pxlfp;
  3432.     seekpost();
  3433.     readpost();
  3434.     if ((tfontptr->c != 0) && (checksum != 0) && (tfontptr->c != checksum))
  3435.     Warning("font = \"%s\",\n-->font checksum = %d,\n-->dvi checksum = %d",
  3436.         tfontptr->name, tfontptr->c, checksum);
  3437.  
  3438.     for(i=FIRSTFNTCHAR; i<=LASTFNTCHAR; i++) {
  3439.     if (char_exists[i]) {
  3440.         tcharptr = &(tfontptr->ch[i]);
  3441.         tcharptr->tfmw = (long)(((float)tfm_wd[i]*(float)tfontptr->s) /
  3442.            (float)((long)1l<<20));
  3443.         tcharptr->where.address.fileOffset = char_pointer[i];
  3444.       }
  3445. #ifdef LJ
  3446. /*                 GF USER PLEASE CHECK IF THIS CODE WORKS
  3447.     tfontptr->max_width = gf_font_max_m;
  3448.     tfontptr->max_height = gf_font_max_n;
  3449.     tfontptr->max_yoff = gf_font_min_n;
  3450. */
  3451. #endif
  3452. #endif
  3453. /*****************************************************************************/
  3454. /*if (tcharptr->charsize==LARGE_SIZE)                                        
  3455.      fprintf(stderr,"%d:\t <%c> w=%d h=%d xO=%d yO=%d tfmw=%ld cw=%ld %d\n",       
  3456.      i,(char) i,                                                          
  3457.      tcharptr->width,tcharptr->height,tcharptr->xOffset,tcharptr->yOffset,
  3458.      tcharptr->tfmw, tcharptr->cw, (int)(tcharptr->charsize));           
  3459.  */
  3460. /*****************************************************************************/
  3461.     }
  3462.   }
  3463.  
  3464.  
  3465. unsigned char
  3466. skip_specials( pkloc )
  3467. long    *pkloc;
  3468. {
  3469.     long    i, j;
  3470.     register unsigned char  flag_byte;
  3471.     do {
  3472.     flag_byte = (unsigned char) NoSignExtend(pxlfp, 1);
  3473. /*
  3474. fprintf(stderr,"flagbyte = %d, pkloc=%ld\n",(int)flag_byte,*pkloc);
  3475. */
  3476.  
  3477.     (*pkloc) ++;
  3478.     if (flag_byte  >= 240)
  3479.         switch (flag_byte) {
  3480.         case 240:
  3481.         case 241:
  3482.         case 242:
  3483.         case 243 : {
  3484.             i = 0 ;
  3485.             for (j = 240; j <= (long)flag_byte; j++) {
  3486.                 i = 256 * i + NoSignExtend(pxlfp, 1) ;
  3487.                 (*pkloc) ++;
  3488.             }
  3489.             for (j = 1; j <= i; j++) {
  3490.                 (void) NoSignExtend(pxlfp, 1) ;
  3491.                 (*pkloc) ++;
  3492.             }
  3493.             break;
  3494.         }
  3495.         case 244 : {
  3496.             i = NoSignExtend(pxlfp, 4);
  3497.             (*pkloc) += 4;
  3498.             break;
  3499.         }
  3500.         case 245 :
  3501.             break;
  3502.         case 246 :
  3503.             break ;
  3504.         case 247:
  3505.         case 248:
  3506.         case 249:
  3507.         case 250:
  3508.         case 251:
  3509.         case 252:
  3510.         case 253:
  3511.         case 254:
  3512.         case 255: {
  3513.             Fatal("Unexpected flagbyte %d!\n",
  3514.                  (int)flag_byte) ;
  3515.             }
  3516.         }
  3517.     } while (!((flag_byte < 240) || (flag_byte == PK_POST))) ;
  3518.     return(flag_byte);
  3519. }
  3520.  
  3521.  
  3522. /*-->ReadPostAmble*/
  3523. /**********************************************************************/
  3524. /**************************  ReadPostAmble  ***************************/
  3525. /**********************************************************************/
  3526. /***********************************************************************
  3527.     This  routine  is  used  to  read  in  the  postamble  values.    It
  3528.     initializes the magnification and checks  the stack height prior  to
  3529.     starting printing the document.
  3530. ***********************************************************************/
  3531. void
  3532. ReadPostAmble(load)
  3533. bool load;
  3534. {
  3535.     FindPostAmblePtr (&postambleptr);
  3536.     if (NoSignExtend(dvifp, 1) != POST)
  3537.         Fatal ("POST missing at head of postamble");
  3538. #ifdef DEBUG
  3539.     if (Debug)
  3540.         fprintf(stderr,"got POST command\n");
  3541. #endif
  3542.     ppagep = NoSignExtend(dvifp, 4);
  3543.     num = NoSignExtend(dvifp, 4);
  3544.     den = NoSignExtend(dvifp, 4);
  3545.     mag = NoSignExtend(dvifp, 4);
  3546.     if ( usermag > 0 && usermag != mag )
  3547.         Warning("DVI magnification of %ld over-ridden by user (%ld)",
  3548.                      mag, usermag );
  3549.     if ( usermag > 0 )
  3550.         mag = usermag;
  3551.     hconv = DoConv(num, den, hconvRESOLUTION);
  3552.     vconv = DoConv(num, den, vconvRESOLUTION);
  3553.     (void) NoSignExtend(dvifp, 4);   /* height-plus-depth of tallest page */
  3554.     (void) NoSignExtend(dvifp, 4);   /* width of widest page */
  3555.     if (NoSignExtend(dvifp, 2) >= STACK_SIZE)
  3556.         Fatal ("Stack size is too small");
  3557.     (void) NoSignExtend(dvifp, 2);   /* this reads the number of pages in */
  3558.                      /* the DVI file */
  3559. #ifdef DEBUG
  3560.     if (Debug)
  3561.         fprintf(stderr,"now reading font defs");
  3562. #endif
  3563.     if (load)
  3564.         GetFontDef ();
  3565. }
  3566.  
  3567.  
  3568. /*-->LoadAChar*/
  3569. /**********************************************************************/
  3570. /***************************** LoadAChar ******************************/
  3571. /**********************************************************************/
  3572. void
  3573. LoadAChar(c, ptr)
  3574. long    c;
  3575. register struct char_entry *ptr;
  3576. {
  3577.     long    *pr;
  3578.     long    bytes;
  3579.  
  3580.     if (ptr->where.address.fileOffset == NONEXISTANT 
  3581. #ifdef LJ_RESIDENT_FONTS
  3582.     || fontptr->resident_p
  3583. #endif
  3584.     ) {
  3585.       ptr->where.isloaded = _FALSE;
  3586.       return;
  3587.     }
  3588.  
  3589.     OpenFontFile();
  3590.  
  3591. #ifdef DEBUG
  3592.     if (Debug)
  3593.       fprintf(stderr, "LoadAChar: <%c>(%ld) from file at pos %ld\n",
  3594.           (char)c,c,ptr->where.address.fileOffset);
  3595. #endif
  3596.  
  3597. #ifdef USEPXL
  3598.  
  3599.     fseek(pxlfp, ptr->where.address.fileOffset, 0);
  3600.  
  3601.     if (fontptr->id == pk89) {
  3602. #ifdef PARANOIA
  3603.         unsigned char   temp;
  3604.         temp = (unsigned char) NoSignExtend(pxlfp, 1);
  3605.  
  3606.         if ((int)(ptr->flag_byte) != (int)temp) {
  3607.            fprintf(stderr,"font=%lx, ptr=%lx\n",fontptr,ptr);
  3608.                 Fatal("%d: oh boy! old flag %d, new flag %d, ptr=%lx\n",
  3609.                           (int)c,(int)(ptr->flag_byte),(int)temp,ptr);
  3610.             }
  3611. #endif
  3612.  
  3613.         if ((ptr->flag_byte & 7) == 7) {
  3614.             bytes = ((long) NoSignExtend(pxlfp, 4)) - 28;
  3615.             fseek(pxlfp, ptr->where.address.fileOffset + 36, 0);
  3616. /*
  3617. fprintf(stderr,"bytes=%d, seeking at %ld\n",
  3618.             bytes, ptr->where.address.fileOffset + 36);
  3619. */
  3620.         } else if ((ptr->flag_byte & 4) == 4) {
  3621.             bytes = ((long)ptr->flag_byte & 3)
  3622.                 * 65536l + NoSignExtend(pxlfp, 2) - 13;
  3623.             fseek(pxlfp, ptr->where.address.fileOffset + 16, 0);
  3624.         } else {
  3625.             bytes = ((long)ptr->flag_byte & 3)
  3626.                 * 256 + NoSignExtend(pxlfp, 1) - 8;
  3627.             fseek(pxlfp, ptr->where.address.fileOffset + 10, 0);
  3628.         }
  3629.     } else if (fontptr->id == id1002)
  3630.         bytes =  ((( (long)ptr->width + 7) >> 3) * (long) ptr->height);
  3631.     else if (fontptr->id == id1001)
  3632.         bytes =  4 * (((long)ptr->width + 31) >> 5) * (long)ptr->height;
  3633.     else 
  3634.         bytes = 0;
  3635.  
  3636.     if (bytes > 0) {
  3637.                                           /* do NOT load Huge characters */
  3638.       if ((bytes > HUGE_CHAR_PATTERN) && (fontptr->id != pk89)) {
  3639.     qfprintf(stderr,"Huge Character <%c> (%ld Bytes)\n", (char)c, bytes);
  3640.         ptr->charsize = HUGE_SIZE;
  3641.         ptr->where.isloaded = _FALSE;
  3642.       } else {
  3643.         if ( (pr = (long *)malloc( bytes )) == NULL )
  3644.             Fatal("Unable to allocate %ld bytes for char <%c>\n",
  3645.                          bytes, (char)c);
  3646. /*
  3647.  * else fprintf(stderr,"allocating %ld bytes char <%c>(%d)\t at 0x%lx\n",
  3648.  *                       bytes, (char)c,(int)c,pr);
  3649.  */ 
  3650. #ifdef DEBUG
  3651.         if (Debug)
  3652.           fprintf(stderr,
  3653.            "Allocating Char <%c>, FileOffset=%lX, Bytes=%ld (%d) <%d>\n",
  3654.               (char) c, ptr->where.address.fileOffset, bytes,
  3655.               (int)bytes, (unsigned int)bytes);
  3656. #endif
  3657.         allocated_storage += bytes;
  3658.         fread(pr, 1, (int) bytes , pxlfp);
  3659.         ptr->where.address.pixptr = pr;
  3660.       }
  3661.     } 
  3662. #else
  3663.     fseek(gfin, ptr->where.address.fileOffset, 0);
  3664.     gettochar();
  3665.     readbits();
  3666.     if (num_bytes > HUGE_CHAR_PATTERN)
  3667.       ptr->charsize = HUGE_SIZE;
  3668. #endif
  3669.     ptr->where.isloaded = _TRUE; 
  3670.     if (ptr->charsize != SMALL_SIZE
  3671. #ifdef LJ
  3672.         /* we might have problems at the edge of the paper with diff. sized characters
  3673.          * the correct treatment would be to check whether the bounding box of 
  3674.          * tfontptr is within the paper relative to the current position  
  3675.          */
  3676.     || fontptr->max_height > CHAR_HEIGTH_LARGE  
  3677.     || (rasterfont[fontptr->plusid])
  3678. #endif
  3679.        )
  3680.         return;
  3681.  
  3682.     EmitChar(c, ptr);
  3683. #ifdef USEPXL
  3684.     /* we should really free the space used by the PXL data after this
  3685.        point, but it is not large, and besides, we may want to be
  3686.        more clever in the future, about sending bitmaps.  So keep
  3687.        the data around */
  3688. #endif
  3689. }
  3690. /*-->SetChar*/
  3691. /**********************************************************************/
  3692. /*****************************  SetChar  ******************************/
  3693. /**********************************************************************/
  3694. void
  3695. SetChar(c, command, PassNo, do_posn,in_string)
  3696. long    c;
  3697. short   command;
  3698. int     PassNo;
  3699. bool do_posn,in_string;
  3700. {
  3701.     register struct char_entry *ptr;  /* temporary char_entry pointer */
  3702.     bool pos_after = _FALSE;
  3703.  
  3704.     ptr = &(fontptr->ch[c]);
  3705.     if (!((ptr->where.isloaded) || (ptr->charsize == HUGE_SIZE)))
  3706.         LoadAChar(c, ptr);
  3707.     if (PassNo == 0)
  3708.         return;
  3709.  
  3710.     if (do_posn) {
  3711. #ifdef IBM3812
  3712.         if (CharStringPos>0) {
  3713.             fprintf(stderr,"!!!! That should never happen!!!\n");
  3714.             CharStringOut;
  3715.         }
  3716. #endif
  3717.         SetPosn(h, v);
  3718.     }
  3719.  
  3720. /*
  3721. printf("(%d) hh=%ld (+%ld/+%ld), h=%ld, xh=%ld,xhh=%ld, [%ld|%ld] ->%d\n",
  3722.     (int)do_posn,hh,(long)ptr->cw,(long)ptr->cw*(long)hconv,h,
  3723.     PIXROUND(h, hconv),
  3724.     PIXROUND(hh, hconv),
  3725.     labs((hh-h)),hconv,(labs((hh-h))>hconv)
  3726.     );
  3727. */
  3728.  
  3729.     if (in_string && (labs((hh-h))>hconv)) {
  3730. #ifdef IBM3812
  3731.         CharStringOut;
  3732. #endif
  3733.         SetPosn(h, v);
  3734.     }
  3735.  
  3736. /*
  3737. fprintf(stderr,"raster?=%d - last_ry=%d, last_rx=%d,mmax-height=%d\n",
  3738.     (int)last_ry < fontptr->max_height, (int)last_ry,(int)last_rx,
  3739.     (int)fontptr->max_height);
  3740. */
  3741.  
  3742.     if (fontptr->font_file_id != NO_FILE) {      /* ignore missing fonts */
  3743.          if (
  3744. #ifdef LJ_RESIDENT_FONTS
  3745.          !fontptr->resident_p && 
  3746. #endif
  3747.          (ptr->charsize != SMALL_SIZE 
  3748. #ifdef LJ
  3749.           /* the LaserJet cannot print characters 
  3750.            * where the bounding box lies outside the 
  3751.            * paper edge. Missing: right paper edge
  3752.            */
  3753.           || last_ry < (int)fontptr->max_height
  3754.           || fontptr->max_height > CHAR_HEIGTH_LARGE
  3755.           || (rasterfont[fontptr->plusid])
  3756. #endif
  3757.           )) {
  3758. #ifdef LJ
  3759.       int     tmp;
  3760.       char    sign;
  3761.       
  3762.       if (!do_posn) {
  3763.         SetPosn(h, v);
  3764.       }
  3765. #ifdef USEPXL
  3766.       tmp = (int) -ptr->yOffset;
  3767. #else
  3768.       tmp = (int) num_rows-y_offset;
  3769. #endif
  3770.       if (tmp != 0) {
  3771.         if (tmp < 0) {
  3772.           sign = '-'; tmp = -tmp;
  3773.         } else
  3774.           sign = '+';
  3775.         EMIT(outfp, "\033*p%c%dY", sign, tmp);
  3776.       }
  3777. #ifdef USEPXL
  3778.       tmp = (int) -ptr->xOffset;
  3779. #else
  3780.       tmp = (int) -x_offset;
  3781. #endif
  3782.       if (tmp != 0) {
  3783.         if (tmp < 0) {
  3784.           sign = '-'; tmp = -tmp;
  3785.         } else
  3786.           sign = '+';
  3787.         EMIT(outfp, "\033*p%c%dX", sign, tmp);
  3788.       }
  3789. #endif
  3790. #ifdef IBM3812
  3791.       CharStringOut;
  3792. #endif
  3793. #ifdef DEBUG
  3794.     if (Debug)
  3795. #ifndef vms
  3796.         fprintf(stderr,"Raster character <%c> %hd\n", (char) c,(short)c);
  3797. #else
  3798.         fprintf(stderr,"Raster character <%c> %d\n", (char) c,(short)c);
  3799. #endif
  3800. #endif
  3801.             RasterChar(ptr);
  3802.             pos_after = _TRUE;
  3803.         } else {
  3804.         unsigned char cc;
  3805.         cc = VisChar((char)c);
  3806. #ifdef IBM3812
  3807. #ifdef USEPXL
  3808.             if ( ptr->yyOffset || (!in_string) ) {
  3809.                 CharStringOut;
  3810.                 MoveVert(ptr->yyOffset);
  3811.                 sprintf(PMPformat, "\01%c", cc);
  3812.                 PMPout(2, PMPformat);
  3813.                 MoveVert((int)-(ptr->yyOffset));
  3814.             } else {
  3815. #endif
  3816.                 if (CharStringPos==CHARSTRINGMAX)
  3817.                     CharStringOut;
  3818.  
  3819.                 CharString[CharStringPos] = cc;
  3820.                 CharStringPos++;
  3821. #ifdef USEPXL
  3822.         }
  3823. #endif
  3824. #endif
  3825. #ifdef LJ
  3826. #define TRANSPARENTCHAR(ch) \
  3827.         if ((ch == 0l) || (ch >= 7l && ch <= 15l) || (ch == 27l)) \
  3828.         EMIT(outfp, "\033&p1X%c", (unsigned char)ch); \
  3829.         else EMITC((unsigned char)ch)
  3830. #ifdef USEPXL
  3831.             if (ptr->yyOffset) {
  3832. #ifndef vms
  3833.                 EMIT(outfp, "\033*p+%hdY", ptr->yyOffset);
  3834.                 TRANSPARENTCHAR(cc);
  3835.                 EMIT(outfp, "\033*p-%hdY", ptr->yyOffset);     /* GUGUGU 255 */
  3836. #else
  3837.                 EMIT(outfp, "\033*p+%dY", ptr->yyOffset);
  3838.                 TRANSPARENTCHAR(cc);
  3839.                 EMIT(outfp, "\033*p-%dY", ptr->yyOffset);     /* GUGUGU 255 */
  3840. #endif
  3841.             } else
  3842. #endif
  3843. /*                EMITC( (unsigned char)c);*/
  3844.                { TRANSPARENTCHAR(cc);}
  3845. #endif
  3846.       }
  3847.         hh += (long) ptr->cw*hconv;
  3848.     }
  3849.     if (command <= SET4)
  3850.         h += ptr->tfmw;
  3851.     if (pos_after) {
  3852.         SetPosn(h, v);
  3853.       }
  3854. }
  3855.  
  3856.  
  3857. void
  3858. DoBop()
  3859. {
  3860.     struct font_entry *p;
  3861. #ifdef LJ
  3862.     register short i;
  3863.     if (fonts_used_on_this_page > MAX_FONTS_PER_PAGE) {
  3864.        for (i = 0; i < HANDLE_MAX_FONTS; i++)
  3865.           rasterfont[i] = _FALSE;
  3866.     }
  3867.     fonts_used_on_this_page = 0;
  3868. #endif
  3869.     for (p = hfontptr; p; p = p->next) {
  3870.         p->used_on_this_page = _FALSE;
  3871.     }
  3872. }
  3873.  
  3874.  
  3875. /*-->SetFntNum*/
  3876. /**********************************************************************/
  3877. /****************************  SetFntNum  *****************************/
  3878. /**********************************************************************/
  3879. void
  3880. SetFntNum(k, Emitting)
  3881. long    k;
  3882. bool Emitting;
  3883. /*  this routine is used to specify the font to be used in printing future
  3884.     characters */
  3885. {
  3886. #ifdef LJ
  3887.     static unsigned short plusid = 0;
  3888. #endif
  3889.     fontptr = hfontptr;
  3890.     while ((fontptr != NULL) && (fontptr->k != k))
  3891.         fontptr = fontptr->next;
  3892.     if (fontptr == NULL)
  3893.         Fatal("font %ld undefined", k);
  3894.     if (Emitting && fontptr->font_file_id != NO_FILE) {
  3895.         if (!fontptr->used_on_this_page
  3896. #ifdef LJ_RESIDENT_FONTS
  3897.         && !fontptr->resident_p
  3898. #endif
  3899.         ) {
  3900.             fontptr->used_on_this_page = _TRUE;
  3901. #ifdef LJ
  3902.             if (++fonts_used_on_this_page > MAX_FONTS_PER_PAGE) {
  3903.               qfprintf(stderr,"this is the %d. font on this page!",
  3904.                        fonts_used_on_this_page);
  3905.               qfprintf(stderr," (max = %d) rastering characters!\n",
  3906.                        MAX_FONTS_PER_PAGE);
  3907.                rasterfont[fontptr->plusid] = _TRUE;
  3908.             }
  3909. #endif
  3910.         }
  3911. #ifdef DEBUG
  3912.     if (Debug)
  3913.       fprintf(stderr, "Switching to font #%ld (%s).\n", k, fontptr->n);
  3914. #endif
  3915.         /* activate font */
  3916. #ifdef IBM3812
  3917.         sprintf(PMPformat, "\323%c", (unsigned char)fontptr->plusid);
  3918.         PMPout(2, PMPformat);
  3919. #endif
  3920. #ifdef LJ
  3921.         if (!rasterfont[fontptr->plusid]) {
  3922. #ifdef LJ_RESIDENT_FONTS
  3923.       if (fontptr->resident_p) {
  3924. #ifdef DEBUG
  3925.             if (Debug)
  3926.               fprintf(stderr, "Resident font #%d.\n", fontptr->resid);
  3927. #endif
  3928.         EMIT(outfp, "\033(%s", fontptr->symbol_set);
  3929.         EMIT(outfp, "\033(s%up%.2f%c%us%db%uT",
  3930.                 fontptr->spacing,
  3931.                 /* height in points, or pitch */
  3932.                 fontptr->spacing ? fontptr->s / 65536.0
  3933.                                          : fontptr->pitch ,
  3934.                         fontptr->spacing ? 'v' : 'h', /* height or pitch? */
  3935.                         fontptr->style,       /* upright, italic, ... */
  3936.                         fontptr->weight,      /* regular, bold, ... */
  3937.                         fontptr->resid);
  3938.       } else
  3939. #endif /* LJ_RESIDENT_FONTS */      
  3940.           if (fontptr->plusid>0) EMIT(outfp, "\033(%dX", fontptr->plusid);
  3941.           else                   EMIT(outfp, "\033(X");
  3942.         }
  3943. /* else fprintf(stderr,"I am doing rasterfont for plusid=%d instead\n",
  3944.                 fontptr->plusid);
  3945. */
  3946. #endif
  3947.     }
  3948. #ifdef LJ    /* reassignment of printer font id  0.48 */
  3949.     else if (fontptr->font_file_id != NO_FILE
  3950. #ifdef LJ_RESIDENT_FONTS
  3951.          && !fontptr->resident_p
  3952. #endif    
  3953.          ) {
  3954.       if (fontptr->ncdl == 0) {
  3955. #ifdef DEBUG
  3956.     if (Debug)
  3957.       fprintf(stderr, "Changing plusid from %d to %d\n", 
  3958.           fontptr->plusid, (int)plusid);
  3959. #endif
  3960.     fontptr -> plusid = plusid;
  3961.     plusid ++;
  3962.       }
  3963.     }
  3964. #endif
  3965. }
  3966.  
  3967.  
  3968. /*-->SetPosn*/
  3969. /**********************************************************************/
  3970. /*****************************  SetPosn  ******************************/
  3971. /**********************************************************************/
  3972. void                  /* output a positioning command */
  3973. SetPosn(x, y)
  3974. long    x, y;
  3975. {
  3976.     int     rx, ry;
  3977.     rx = (int)PIXROUND(x, hconv) + x_goffset;
  3978.     ry = (int)PIXROUND(y, vconv) + y_goffset;
  3979.  
  3980. /*
  3981. * fprintf(stderr,"setposn to %ld/%ld, %d/%d\n",x,y,rx,ry);
  3982. */
  3983.  
  3984. #ifdef IBM3812
  3985.     PMPcont(3);
  3986.     PMPoutC('\340');
  3987.     EMITWORD(LARGER(rx,0));
  3988.  
  3989.     if (last_ry != ry) { /* necessary to set new y-position */
  3990.         PMPcont(3);
  3991.         PMPoutC('\341');
  3992.         EMITWORD(LARGER(ry,0));
  3993.     }
  3994. #endif
  3995. #ifdef LJ
  3996.     if (last_ry != ry)   /* necessary to set new y-position */
  3997.         EMIT(outfp, "\033*p%dx%dY", LARGER(rx,0), LARGER(ry,0));
  3998.     else
  3999.         EMIT(outfp, "\033*p%dX", LARGER(rx,0));
  4000. #endif
  4001.  
  4002.     last_ry = ry;    /* last y-position on output device */
  4003.     last_rx = rx;    /* last x-position on output device */
  4004. /*
  4005.  * must know where device "really" is horizontally, for rel. posning.
  4006.  * (maybe in the future), but we always use direct positioning for
  4007.  * vertical movement.
  4008.  */
  4009.     /* hh = rx * hconv; */
  4010.     hh = x;
  4011.     vv = y;
  4012. /*
  4013.  *     fprintf(stderr,"DoPosn: x=%ld, y=%ld, rx=%d, ry=%d, hh=%ld, vv=%ld\n",
  4014.  *               x,y,rx,ry,hh,vv);
  4015.  */
  4016. }
  4017.  
  4018.  
  4019. #ifdef IBM3812
  4020. /*-->PMPLine*/
  4021. /**********************************************************************/
  4022. /*****************************  PMPLine  ******************************/
  4023. /**********************************************************************/
  4024. void       /* drawing lines on the 3812 using PMP vector commands */
  4025. PMPLine(w, y, x)
  4026. int     w, y, x;
  4027. {
  4028.  
  4029.     if ((w == 0) || ((x == 0) && (y == 0)))
  4030.         return;
  4031.  
  4032. /*
  4033. fprintf(stderr,"w=%d / %d - %d, y=%d / %d - %d, x=%d / %d - %d\n",
  4034.         w,(char)(w & 0xff),(int)((signed_char)(w & 0xff)),
  4035.         y,(char)(y & 0xff),(int)((signed_char)(y & 0xff)),
  4036.         x,(char)(x & 0xff),(int)((signed_char)(x & 0xff)));
  4037. */
  4038.  
  4039.     if ( (((signed_char)(x & 0xff)) == x ) &&
  4040.         ( ((signed_char)(y & 0xff)) == y ) ) {
  4041.         PMPcont(6);
  4042.         PMPout(1, "\370");
  4043.         EMITWORD(3);      /* length of vector */
  4044.         PMPoutC((unsigned char)(0x80 | 0x00 | (unsigned char) w));
  4045.         PMPoutC((signed_char)(y & 0xff));
  4046.         PMPoutC((signed_char)(x & 0xff));
  4047. /*
  4048.         fprintf(stderr,"F8 00 03: w=%d, x=%d(%d-%.2X), y=%d(%d-%.2X),\n",
  4049.         w,x,(char)(x & 0xff),(signed_char)(x & 0xff),
  4050.           y,(char)(y & 0xff),(signed_char)(y & 0xff));
  4051. */
  4052.  
  4053.     } else {
  4054.         PMPcont(8);
  4055.         PMPout(1, "\370");
  4056.         EMITWORD(4 + 1);      /* length of vector */
  4057.         PMPoutC((unsigned char)(0xC0 | 0x00 | (unsigned char) w));
  4058.         EMITWORD(y);
  4059.         EMITWORD(x);
  4060. /*
  4061.         fprintf(stderr,"F8 00 05: w=%d, x=%d, y=%d,\n", w,x,y);
  4062. */
  4063.     }
  4064. }
  4065.  
  4066.  
  4067. #endif
  4068. /*-->SetRule*/
  4069. /**********************************************************************/
  4070. /*****************************  SetRule  ******************************/
  4071. /**********************************************************************/
  4072. void                   /*   this routine will draw a rule */
  4073. SetRule(a, b, Set)
  4074. long    a, b;
  4075. int     Set;
  4076. {
  4077.     long    xx, yy;
  4078. #ifdef IBM3812
  4079.     short   hor_offset, vert_offset, ll;
  4080. #endif
  4081.     if ( a > 0 && b > 0 ) {
  4082.         SetPosn(h, v);             /* lower left corner */
  4083.         xx = (long)PIXROUND(b, hconv);     /* width */
  4084.         yy = (long)PIXROUND(a, vconv);     /* height */
  4085.  
  4086. #ifdef DEBUG
  4087.         if (Debug)
  4088.             fprintf(stderr,"Rule xx=%ld, yy=%ld\n", xx, yy);
  4089. #endif
  4090.  
  4091. #ifdef IBM3812
  4092.         hor_offset  = (short)(last_ry - yy);
  4093.         if (hor_offset < 0) yy += hor_offset;
  4094.         if (last_rx < 0) xx += last_rx;
  4095.  
  4096.         if (Landscape) {
  4097.           if (last_ry > MAX_PAGE_WIDTH) yy += MAX_PAGE_WIDTH-last_ry;
  4098.           hor_offset  = (short)(MAX_PAGE_HEIGHT - (last_rx + xx));
  4099.         } else {
  4100.           if (last_ry > MAX_PAGE_HEIGHT) yy += MAX_PAGE_HEIGHT-last_ry;
  4101.           hor_offset  = (short)(MAX_PAGE_WIDTH - (last_rx + xx));
  4102.         }
  4103.         if (hor_offset < 0) xx += hor_offset;
  4104.  
  4105.         if ((xx > 31) && (yy > 31)) {
  4106. /*
  4107.  *   fill area by multiple lines  (kind of a mess)
  4108.  *   process for simplicity always horizontally
  4109.  */
  4110.  
  4111. /* fprintf(stderr, "large box: w=%d, x=%d, y=%d\n",(int)yy,(int)xx,0);*/
  4112.  
  4113.             hor_offset  = HOR_HALF(30);
  4114.             MoveHor(hor_offset);
  4115.             vert_offset = VERT_HALF(30);
  4116.             MoveVert(-vert_offset);
  4117.             ll = (short)xx - 30;
  4118.  
  4119.             for (; yy > 30; yy -= 30) {
  4120.                 PMPLine(30, 0, ll);
  4121.                 MoveHor(-ll);
  4122.                 MoveVert(-30);
  4123.             }
  4124.  
  4125.             hor_offset  = -hor_offset     + HOR_HALF(yy);
  4126.             MoveHor(hor_offset);
  4127.             vert_offset = (vert_offset - 30) + VERT_HALF(yy);
  4128.             MoveVert(-vert_offset);
  4129.  
  4130.             PMPLine((int)yy, 0, (int)(xx - yy));
  4131.  
  4132.         } else if ( (yy < xx) && (xx > 0) ) {
  4133.  
  4134. /* fprintf(stderr, "hori rule: w=%d, x=%d, y=%d\n",(int)yy,(int)(xx-yy),0);*/
  4135.  
  4136.             hor_offset  = HOR_HALF(yy);
  4137.             vert_offset = VERT_HALF(yy);
  4138.  
  4139.             MoveHor(hor_offset);
  4140.             MoveVert(-vert_offset);
  4141.  
  4142.             PMPLine((int)yy, 0, (int)(xx - yy));
  4143.         } else if ( (xx < yy) && (yy > 0)) {
  4144.  
  4145.             hor_offset  = HOR_HALF(xx);
  4146.             vert_offset = VERT_HALF(xx);
  4147. /*
  4148.  fprintf(stderr, "move: x=%d, y=%d\n",hor_offset,-vert_offset);
  4149.  fprintf(stderr, "vert rule: w=%d, x=%d, y=%d\n",(int)xx,0,(int)-(yy-xx));
  4150. */
  4151.             MoveHor(hor_offset);
  4152.             MoveVert(-vert_offset);
  4153.  
  4154.             PMPLine((int)xx, (int)-(yy - xx), 0);
  4155.         } else if (xx == yy) {
  4156.             short     y0;  /* small square box!! */
  4157.  
  4158.             y0 = (short)yy / 2;
  4159.             hor_offset  = HOR_HALF(y0);
  4160.             MoveHor(hor_offset);
  4161.             vert_offset = VERT_HALF(y0);
  4162.             MoveVert(-vert_offset);
  4163.             ll = (short)xx - y0;
  4164.  
  4165.             PMPLine((int)y0, 0, ll);
  4166.  
  4167.             hor_offset  = -(ll + hor_offset);
  4168.             vert_offset = (y0 - vert_offset);
  4169.  
  4170.             yy -= (long)y0;
  4171.             hor_offset  += HOR_HALF(yy);
  4172.             MoveHor(hor_offset);
  4173.             vert_offset += VERT_HALF(yy);
  4174.             MoveVert(-vert_offset);
  4175.  
  4176.             PMPLine((int)yy, 0, (int)xx - yy);
  4177.         }
  4178. #endif
  4179. #ifdef LJ
  4180.     if (last_ry + 1 < yy) yy = last_ry + 1;
  4181.     if (last_rx < 0) xx += last_rx;
  4182.  
  4183.     if (((int)pgsiz_dots >0) && ((int)last_ry > (int)pgsiz_dots))
  4184.       yy += (long)pgsiz_dots - (long)last_ry;
  4185.  
  4186.         if ((yy>0) && (xx>0))
  4187.                 EMIT(outfp, "\033*p-%ldY\033*c%lda%ldbP", yy - 1, xx, yy);
  4188. #endif
  4189.         last_ry = UNKNOWN;       /* next time full positioning */
  4190.     }
  4191.     if (Set)
  4192.         h += b;
  4193. }
  4194.  
  4195.  
  4196. /*-->SetString*/
  4197. /**********************************************************************/
  4198. /*****************************  SetString  ****************************/
  4199. /**********************************************************************/
  4200. void
  4201. SetString(firstch, PassNo)    /* read and set a consecutive string of chars */
  4202. short   firstch;
  4203. int     PassNo;
  4204. {
  4205.     short   c;
  4206.     register unsigned short i;
  4207.  
  4208. #ifdef DEBUG
  4209.     if (Debug)
  4210.       fprintf(stderr, "SetString ");
  4211. #endif
  4212.     for (i = 0, c = firstch; c >= SETC_000 && c <= SETC_127; i++) {
  4213. #ifdef DEBUG
  4214.         if (Debug)
  4215.           fprintf(stderr, "%d(%c) ", c, c);
  4216. #endif
  4217.         SetChar((long)c,  c, PassNo, (bool)(i==0),_TRUE);
  4218.         c = (short) NoSignExtend(dvifp, 1);
  4219.     }
  4220.     fseek(dvifp, -1l, 1);    /* backup one character */
  4221. #ifdef IBM3812
  4222.     CharStringOut;
  4223. #endif
  4224. #ifdef DEBUG
  4225.     if (Debug)
  4226.       fprintf(stderr, "...SetString\n");
  4227. #endif
  4228. }
  4229.  
  4230. #ifndef ARITHMETIC_RIGHT_SHIFT
  4231. long   signTab[5] = {0,0x00000080,0x00008000,0x00800000,0x00000000};
  4232. long extendTab[5] = {0,~0^0xff,~0^0xffff,~0^0xffffff,~0^0xffffffff};
  4233. #endif
  4234.  
  4235. /*-->SignExtend*/
  4236. /**********************************************************************/
  4237. /****************************  SignExtend  ****************************/
  4238. /**********************************************************************/
  4239. long
  4240. SignExtend(fp, n)   /* return n byte quantity from file fd */
  4241. register FILE *fp;  /* file pointer    */
  4242. register int    n;  /* number of bytes */
  4243. {
  4244.     int     n1;     /* number of bytes      */
  4245.     long    x;      /* number being constructed */
  4246. #ifdef SIGN_DEBUG
  4247.     long    x0;     /* copy of x  */
  4248. #endif
  4249.     x = getc(fp);   /* get first (high-order) byte */
  4250.     n1 = n--;
  4251.     while (n--)  {
  4252.         x <<= 8;
  4253.         x |= getc(fp);
  4254.     }
  4255. /*
  4256.  *   NOTE: This code assumes that the right-shift is an arithmetic, rather
  4257.  *   than logical, shift which will propagate the sign bit right.   According
  4258.  *   to Kernighan and Ritchie, this is compiler dependent!
  4259.  */
  4260.  
  4261. #ifdef SIGN_DEBUG
  4262.     x0 = x;
  4263. #endif
  4264.  
  4265. #ifdef ARITHMETIC_RIGHT_SHIFT
  4266.     x <<= 32 - 8 * n1;
  4267.     x >>= 32 - 8 * n1; /* sign extend */
  4268. #else
  4269.     if (x & signTab[n1]) x |= extendTab[n1];
  4270. #endif
  4271.  
  4272. #ifdef SIGN_DEBUG
  4273.     fprintf(stderr,"\tSignExtend(fp,%d)=%lX, was=%lX,%d\n",
  4274.         n1,x,x0,x0&signTab[n1]);
  4275. #endif
  4276.  
  4277. #ifdef DEBUG
  4278.     if (Debug > 1)
  4279.         fprintf(stderr,"\tSignExtend(fp,%d)=%lX\n", n1, x);
  4280. #endif
  4281.     return(x);
  4282. }
  4283.  
  4284.  
  4285. /*-->SkipFontDef*/
  4286. /**********************************************************************/
  4287. /****************************  SkipFontDef  ***************************/
  4288. /**********************************************************************/
  4289. void
  4290. SkipFontDef()
  4291. {
  4292.     int     a, l;
  4293.     char    n[STRSIZE];
  4294.  
  4295.     (void) NoSignExtend(dvifp, 4);
  4296.     (void) NoSignExtend(dvifp, 4);
  4297.     (void) NoSignExtend(dvifp, 4);
  4298.     a = (int) NoSignExtend(dvifp, 1);
  4299.     l = (int) NoSignExtend(dvifp, 1);
  4300.     GetBytes(dvifp, n, a + l);
  4301. }
  4302.  
  4303.  
  4304. /*-->Warning*/
  4305. /**********************************************************************/
  4306. /*****************************  Warning  ******************************/
  4307. /**********************************************************************/
  4308. void                           /* issue a warning */
  4309. Warning(fmt, a, b, c, d)
  4310. char    *fmt;         /* format    */
  4311. char    *a, *b, *c, *d;   /* arguments */
  4312. {
  4313. #ifndef vms
  4314.     G_errenc = 1;
  4315. #else
  4316.     G_errenc = (SS$_ABORT | STS$M_INHIB_MSG);  /* no message on screen */
  4317. #endif
  4318.     if ( G_nowarn || G_quiet )
  4319.         return;
  4320.  
  4321.     fprintf(stderr, "%s: warning: ", G_progname);
  4322.     fprintf(stderr, fmt, a, b, c, d);
  4323.     fprintf(stderr, "\n");
  4324. }
  4325.  
  4326. void
  4327. PutWord(w)
  4328. int     w;
  4329. {
  4330.     EMITC((char)(w >> 8) & 0xff);
  4331.     EMITC((char)w & 0xff);
  4332. }
  4333.  
  4334.  
  4335. #ifdef IBM3812
  4336. /*-->PMPout*/
  4337. /*****************************************************************************/
  4338. /* This routine produces the PMP-envelopes for the 3812. Its semantics are:
  4339.  
  4340.    first arg == 0  ... flush buffer
  4341.    first arg == -1 ... number of bytes specified in the second argument
  4342.                have to be continuous, that is they must not
  4343.                be disrupted by ENTER PMP etc.
  4344.    first arg > 0       output first arg bytes
  4345.  
  4346.                If arg2 > OUTBUFSIZE ... flush buffer,
  4347.                         switch to unbuffered mode
  4348.                         (dont't collect PMP commands)
  4349.                If arg2+bufferpointer > OUTBUFSIZE ... flush buffer,
  4350.                         block will fit into buffer
  4351.                otherwise ..... block will fit into buffer
  4352.  
  4353.   Buffering is done to reduce the ENTER PMP-commands. Initially
  4354.   the 3812 is in PC-ASCII mode. In order to issue a PMP-command it is
  4355.   necessary to enter PMP mode. The ENTER-PMP-command contains the
  4356.   number of bytes that will be interpreted as PMP-commands. In the
  4357.   most naive approach for each primitive command (eg. set cursor) you
  4358.   have to produce a seperate ENTER-PMP-envelope (5 bytes). It is
  4359.   favourable to collect as many PMP commands as possible in one envelope. */
  4360. /*****************************************************************************/
  4361. void
  4362. PMPout(l, s)
  4363. char    *s;
  4364. int     l;
  4365. {
  4366.     static char     buffer[OUTBUFSIZE];
  4367.     static unsigned short   bp = 0;         /* range 0..OUTBUFSIZE */
  4368.     static long     continuous = 0l;
  4369.     static bool buffered = _TRUE;
  4370.  
  4371.     if (l == 0) {
  4372.         if (bp == 0)
  4373.             return;
  4374.         EMIT(outfp, "\033[C%c%c", (unsigned char)(bp & 0xFF),
  4375.             (unsigned char)(bp >> 8));
  4376.         EMITB((int)bp, buffer);
  4377.         bp = 0;
  4378.         return;
  4379.     }
  4380.     if (l == -1) {
  4381.         continuous = (long)s;
  4382.         if (continuous + (long)bp + 5l > (long) OUTBUFSIZE)
  4383.             PMPflush;
  4384.         buffered = (bool) ((continuous + 5l <= (long) OUTBUFSIZE));
  4385.         if (!buffered) {
  4386.             EMIT(outfp, "\033[C%c%c",
  4387.                 (unsigned char)(continuous & 0xFF),
  4388.                 (unsigned char)((continuous >> 8) & 0xFF));
  4389.         }
  4390.         return;
  4391.     }
  4392.     if (buffered) {
  4393.         register int    i;
  4394.         if ( ((long)l + bp) > OUTBUFSIZE)
  4395.             PMPflush;
  4396.         for (i = 0; i < l; i++)
  4397.             buffer[bp+i] = s[i];
  4398.         bp += (unsigned short)l;
  4399.     } else {
  4400.         EMITB((int)l, s);
  4401.         buffered = (bool) ((continuous -= (long)l) <= 0) ;
  4402.     }
  4403. }
  4404.  
  4405.  
  4406. void
  4407. PMPoutC(c)
  4408. char    (c);
  4409. {
  4410.     PMPout(1, &c);
  4411. }
  4412.  
  4413.  
  4414. #endif
  4415. #ifdef MSDOS
  4416. /*-->AssureBinary*/
  4417. /**********************************************************************/
  4418. /*************************** AssureBinary *****************************/
  4419. /**********************************************************************/
  4420. /* This procedure is both DOS AND MSC dependent. The MSC file open on */
  4421. /* a device ignores the 'binary' of the "wb" parameter and opens the  */
  4422. /* file in ascii mode. This procedure sets the file f to binary mode  */
  4423. /* if it is connected to a device that is not console input or output */
  4424. /* or the null device. For other operating systems this routine is    */
  4425. /* useless. (Background: MSDOS 3.2 Technical Reference upd 1 pg 6-137 */
  4426. /**********************************************************************/
  4427. void
  4428. AssureBinary(f)
  4429. FILE *f;
  4430. {
  4431.     union REGS regs;              /* registers for bios call */
  4432.  
  4433.     regs.h.ah = (unsigned char) 0x44;     /* IOCTL            */
  4434.     regs.h.al = (unsigned char) 0x00;     /* get device information   */
  4435.     regs.x.bx = (unsigned int) fileno(f); /* handle from MSC      */
  4436.     intdos(®s, ®s);         /* call DOS interrupt       */
  4437.                           /* ---> result in DX    */
  4438.  
  4439.     if (  (regs.h.dl & 0x80)     /* file handle points to a device */
  4440.          && !(regs.h.dl & 0x07) )    /* neither console i/o or null    */ {
  4441.  
  4442.         regs.h.dl  |= 0x20;      /* set BINARY bit in device info  */
  4443.  
  4444.         regs.h.ah = (unsigned char) 0x44;    /* IOCTL         */
  4445.         regs.h.al = (unsigned char) 0x01;    /* set device information*/
  4446.         regs.x.bx = (unsigned int) fileno(f); /* handle from MSC      */
  4447.         regs.h.dh = (unsigned char) 0x00;    /* clear DH          */
  4448.         intdos(®s, ®s);           /* call DOS interrupt     */
  4449.     }
  4450. }
  4451.  
  4452.  
  4453. #endif
  4454.  
  4455. #ifdef USEPXL
  4456. bool getbit ();
  4457. unsigned char   getnyb ();
  4458. long    pk_packed_num ();
  4459.  
  4460.  
  4461. #define  PKBYTE   *pkloc; pkloc ++
  4462. #define  OUTCHAR(c) raster_line_buf[bp]= (unsigned char)c; bp++
  4463.  
  4464. unsigned char   bitweight, inputbyte ;
  4465. unsigned char   dyn_f ;
  4466. unsigned char   *pkloc;
  4467. int     repeatcount;
  4468.  
  4469. void              /* <Read and translate raster description@>*/
  4470. PkRaster(ce, raster)
  4471. struct char_entry *ce;
  4472. bool raster;
  4473. {
  4474.     int     rp;
  4475.     int     current_line;
  4476.     int     wordwidth ;
  4477.     bool turnon;
  4478.     unsigned short  nbpl;
  4479.     long    rowsleft, word, wordweight, hbit, count, i, j, tl;
  4480.     long    row[101] ;
  4481.     unsigned char   raster_line_buf[BYTES_PER_PIXEL_LINE];
  4482.     unsigned short  bp;
  4483.  
  4484.  
  4485.     if (ce->charsize == HUGE_SIZE)
  4486.         Fatal( "cannot process currently PK font patterns of that size!\n");
  4487.  
  4488.  
  4489.     current_line = 0;
  4490.     pkloc = (unsigned char *)ce->where.address.pixptr;
  4491.     dyn_f = (unsigned char)(ce->flag_byte >> 4);
  4492.     turnon = (bool)((ce->flag_byte & 8) == 8);
  4493.     wordwidth = (int)(ce->width + 31) >> 5 ;
  4494.     nbpl = ((int)(ce->width) +  7) >> 3;
  4495.  
  4496.     bitweight = 0 ;
  4497.     if (dyn_f == 14) {
  4498.         /*printf("<Get raster by bits@>\n");*/
  4499.         for (i = 1; i <= (long)ce->height; i++) {
  4500.         word = 0 ;
  4501.         wordweight = 31 ;
  4502.         bp = 0;            /* Sowa */
  4503.  
  4504. #ifdef DRAWGLYPH
  4505.            printf("     |");
  4506. #endif
  4507.         for (j = 1; j <= (long) ce->width; j++) {
  4508.             bool getbit;
  4509.             /* bp = 0;               SOWA */
  4510. /*******************************************begin Getbit *********/
  4511.             bitweight /= 2 ;
  4512.             if ( bitweight == 0 ) {
  4513.                 inputbyte = PKBYTE ;
  4514.                 bitweight = 128 ;
  4515.             }
  4516.             getbit = (bool)
  4517.              ( inputbyte >= bitweight ) ;
  4518.             if ( getbit )
  4519.                 inputbyte -= bitweight ;
  4520. /*********************************************end Getbit *********/
  4521.  
  4522.             if (getbit)
  4523.                 word += power[wordweight] ;
  4524.  
  4525.             wordweight --;
  4526.             if (wordweight == -1) {
  4527.  
  4528. #ifdef DRAWGLYPH
  4529.    { int k;
  4530.      for (k=31; k>=0; k--) {
  4531.          if ((power[k] & word)!=0) printf("M");
  4532.          else printf(".");
  4533.      }
  4534.    }
  4535. #endif
  4536.  
  4537.             OUTCHAR((word >> 24 & 0xFF));
  4538.             OUTCHAR((word >> 16 & 0xFF));
  4539.             OUTCHAR((word >> 8 & 0xFF));
  4540.             OUTCHAR((word    & 0xFF));
  4541.  
  4542.             word = 0 ;
  4543.             wordweight = 31 ;
  4544.             }
  4545.         }
  4546.         if (wordweight < 31) {
  4547. #ifdef COMMENT
  4548. #ifdef DRAWGLYPH
  4549.    { int k;
  4550.      for (k=15; k>=0; k--) {
  4551.         if ((power[k] & word)!=0) printf("Z");
  4552.         else printf(":");
  4553.      }
  4554.     }
  4555.     printf("|\n ----|");
  4556. #endif
  4557. #endif
  4558.  
  4559.             for (j = 3; j >= (wordwidth * 4 - (long)nbpl);
  4560.             j--) {
  4561.  
  4562.                 OUTCHAR(((word >> (j << 3)) & 0xff));
  4563.  
  4564. #ifdef DRAWGLYPH
  4565.    { int k;
  4566.      for (k=7; k>=0; k--) {
  4567.         if ((power[k] & ((word >> (j << 3)) & 0xff))!=0) printf("M");
  4568.         else printf(".");
  4569.      }
  4570.    }
  4571. #endif
  4572.  
  4573.             }
  4574.         }
  4575.  
  4576.         if (raster) {
  4577.             RasterLine(ce, (unsigned int)nbpl,
  4578.                 current_line, raster_line_buf);
  4579.             current_line++;
  4580.         } else
  4581.             EMITL(bp, raster_line_buf);
  4582.  
  4583. #ifdef DRAWGLYPH
  4584.    printf("|\n");
  4585. #endif
  4586.         }
  4587.     } else {
  4588.         /* fprintf(stderr, "@<Create normally packed raster@>\n"); */
  4589.         rowsleft = (long) ce->height ;
  4590.         hbit = (long) ce->width ;
  4591.         repeatcount = 0 ;
  4592.         wordweight = 32 ;
  4593.         word = 0 ;
  4594.         rp = 1 ;
  4595.         while ( rowsleft > 0 ) {
  4596.         count = pk_packed_num() ;
  4597.         bp = 0;
  4598.  
  4599.         while (count > 0) {
  4600.             if ((count < wordweight) && (count < hbit)) {
  4601.             if (turnon)
  4602.                 word +=
  4603.                     gpower[wordweight] -
  4604.                     gpower[wordweight - count] ;
  4605.  
  4606.             hbit -= count ;
  4607.             wordweight -= count ;
  4608.             count = 0 ;
  4609.             } else if ((count >= hbit) && (hbit <=
  4610.             wordweight)) {
  4611.  
  4612.             if (turnon)
  4613.                 word +=
  4614.                     gpower[wordweight] -
  4615.                     gpower[wordweight - hbit] ;
  4616.  
  4617.             row[rp] = word ;
  4618.  
  4619.             /*fprintf(stderr, " @<Send row@> \n");*/
  4620.             for (i = 0; i <= (long) repeatcount; i++) { int ii;
  4621.  
  4622. #ifdef DRAWGLYPH
  4623.   printf("***  |");
  4624. #endif
  4625.                 for (ii = 1; ii < wordwidth; ii++) {
  4626.                 tl = row[ii];
  4627.  
  4628.                 OUTCHAR((tl >> 24 & 0xFF));
  4629.                 OUTCHAR((tl >> 16 & 0xFF));
  4630.                 OUTCHAR((tl >> 8  & 0xFF));
  4631.                 OUTCHAR((tl       & 0xFF));
  4632.  
  4633. #ifdef DRAWGLYPH
  4634.    { int k;
  4635.      for (k=31; k>=0; k--)  {
  4636.          if ((power[k] & row[ii])!=0) printf("M");
  4637.          else printf(".");
  4638.      }
  4639.    }
  4640. #endif
  4641.                 }
  4642.                 tl = row[wordwidth];
  4643.                 for (j = 3; j >= (wordwidth *4 - (long)nbpl);
  4644.                  j--) {
  4645.  
  4646.                  OUTCHAR(((tl >> (j << 3)) & 0xff));
  4647.  
  4648. #ifdef DRAWGLYPH
  4649.    { int k;
  4650.      for (k=7; k>=0; k--) {
  4651.          if ((power[k] & ((tl >> (j << 3)) & 0xff))!=0) printf("M");
  4652.          else printf(".");
  4653.      }
  4654.    }
  4655. #endif
  4656.                 }
  4657.  
  4658.                 if (raster) {
  4659.                     RasterLine(ce,
  4660.                     (unsigned int)nbpl,
  4661.                      current_line,
  4662.                      raster_line_buf);
  4663.                     current_line++;
  4664.                 } else
  4665.                     EMITL(bp, raster_line_buf);
  4666.  
  4667.                 bp = 0;
  4668.  
  4669. #ifdef DRAWGLYPH
  4670.    printf("|  ");
  4671.    for (j=1;j<=(long)wordwidth;j++) printf("%02lX/",row[j]);
  4672.    printf(" raster=%d\n",raster);
  4673. #endif
  4674.             }
  4675.  
  4676.             rowsleft -=  (long)repeatcount + 1 ;
  4677.             repeatcount = 0 ;
  4678.             rp = 1 ;
  4679.             word = 0 ;
  4680.             wordweight = 32 ;
  4681.             count -= hbit ;
  4682.             hbit = (long)ce->width ;
  4683.             } else {
  4684.             if (turnon) word += gpower[wordweight] ;
  4685.             row[rp] = word ;
  4686.             rp = rp + 1 ;
  4687.             word = 0 ;
  4688.             count -= wordweight ;
  4689.             hbit -= wordweight ;
  4690.             wordweight = 32 ;
  4691.             }
  4692.         }   /* .....while count > 0 */
  4693.         if (turnon)
  4694.             turnon = _FALSE;
  4695.         else
  4696.             turnon = _TRUE;
  4697.         } /* ...... rowsleft > 0 */
  4698.         if ((rowsleft != 0) || (hbit != (long)ce->width))
  4699.             Fatal("Bad pk file----more bits than required!\n");
  4700.     } /* .... create normally packed raster */
  4701. }
  4702.  
  4703.  
  4704. unsigned char   getnyb ()
  4705. {
  4706.     register unsigned char  temp ;
  4707.     if ( bitweight == 0 ) {
  4708.         inputbyte = PKBYTE ;
  4709.         bitweight = 16 ;
  4710.     }
  4711.     temp = inputbyte / bitweight ;
  4712.     inputbyte -= temp * bitweight ;
  4713.     bitweight /= 16 ;
  4714.     return ( temp ) ;
  4715. }
  4716.  
  4717.  
  4718. long
  4719. pk_packed_num ()
  4720. { /*@<Packed number procedure@>= */
  4721.     register int    i;
  4722.     long    j;
  4723.  
  4724.     i = (int)getnyb();
  4725.     if (i == 0) {
  4726.         do {
  4727.             j = (long)getnyb();
  4728.             i++;
  4729.         } while (j == 0) ;
  4730.         while (i > 0) {
  4731.             j = j * 16 + (long)getnyb() ;
  4732.             i--;
  4733.         };
  4734.         return (j - 15 + (13 - dyn_f) * 16 + dyn_f) ;
  4735.     } else if (i <= (int)dyn_f) {
  4736.         return ((long)i);
  4737.     } else if (i < 14) {
  4738.         return ((i-(long)dyn_f - 1) * 16 + (long)getnyb() + dyn_f + 1);
  4739.     } else {
  4740.         if (i == 14) {
  4741.             repeatcount = (int) pk_packed_num() ;
  4742.         } else {
  4743.             repeatcount = 1 ;
  4744.         }
  4745.         /*     fprintf(stderr,"repeatcount = [%d]\n",repeatcount);    */
  4746.         return (pk_packed_num()) ;    /* tail end recursion !! */
  4747.     }
  4748. }
  4749. #endif  
  4750.  
  4751. #ifndef USEPXL
  4752. void bad_gf(n)
  4753.     int n;
  4754. {
  4755.     Fatal("Bad gf file, case %d\n",n);      /* See gf.c */
  4756. }
  4757. #endif
  4758. /*-->FormFeed*/
  4759. /**********************************************************************/
  4760. /*****************************  FormFeed ******************************/
  4761. /**********************************************************************/
  4762. void
  4763. FormFeed()
  4764. {
  4765.  
  4766. #ifdef IBM3812
  4767.     unsigned short pages;
  4768.     if ( (ndone == 0) && (FirstAlternate)){
  4769.         for (pages = 1; pages < ncopies; pages++) {
  4770.             PMPout(2, "\321\300"); /* PMP-command xD1C0 */
  4771.         }
  4772.         PMPout(2, "\321\100"); /* PMP-command xD140 */
  4773.     } else {
  4774.         for (pages = 1; pages < ncopies; pages++){
  4775.             PMPout(2, "\321\200"); /* PMP-command xD180 */
  4776.         }
  4777.         PMPout(2, "\321\0"); /* PMP-command xD100 */
  4778.     }
  4779. #endif
  4780. #ifdef LJ
  4781.     EMITC('\f');
  4782. #endif
  4783. }
  4784.